Two-liner to one-liner

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Two-liner to one-liner

    Hi all,

    I tried to stuff the following two lines into one, but did not succeed:

    my($teilnehmer) = $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;
    my(@teilzeilen) = split /\n/, $teilnehmer;

    This does not work:

    my @teilnehmer = $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;

    This does neither:

    my @teilnehmer = split /\n/, $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;

    Any ideas?

    Thanks,

    Jan
    --
    How many Microsoft engineers does it take to screw in a lightbulb? None. They just redefine "dark" as the new standard.
    Jan Eden Guest

  2. Similar Questions and Discussions

    1. scrambler one-liner
      I just came across this interesting article at Slashdot that explains that according to some English university the order of letters in words are...
    2. eval parameters one liner
      I know I saw something like this in the past I just can't find it. Anyone got any ideas? perl -e 'print eval { @ARGV }, "\n"' 5 + 5 it should...
    3. Hm... nice, Euclid is a one-liner
      a, b = b, a % b while b != 0 That's what I like about Ruby. The Perl equivalent is, of course: ($a, $b) = ($b, $a % $b) while $b; Is...
    4. can this one-liner be squeezed any shorter?
      On Sun, 27 Jul 2003, Dan Jacobson wrote: Well, you ARE hardwiring $q's value. Or do you mean using the expression instead of $q? You...
    5. Need a quick one-liner
      Hi everybody I have some data files in the following format: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ....
  3. #2

    Default Re: Two-liner to one-liner

    Jan Eden wrote:
    >
    > Hi all,
    Hello,
    > I tried to stuff the following two lines into one, but did not succeed:
    >
    > my($teilnehmer) = $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;
    > my(@teilzeilen) = split /\n/, $teilnehmer;
    >
    > This does not work:
    >
    > my @teilnehmer = $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;
    >
    > This does neither:
    >
    > my @teilnehmer = split /\n/, $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;
    >
    > Any ideas?

    my @teilnehmer = map /.+/g, $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  4. #3

    Default RE: Two-liner to one-liner

    Jan Eden <lists@jan-eden.de> wrote:
    :
    : I tried to stuff the following two lines into one, but did
    : not succeed:
    :
    : my($teilnehmer) = $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;
    : my(@teilzeilen) = split /\n/, $teilnehmer;

    Care to share a typical value for $eingabe with us?

    How about:

    my @teilzeilen = split /\n/, ( $eingabe =~ /(?<=Teilnehmer:\n\n)(.+)/s )[0];

    I think(?) "( $eingabe =~ /(?<=Teilnehmer:\n\n)(.+)/s )[0]"
    is forcing the regex into list context. In scalar context it
    returns 1 (for success?) and split assumes scalar context of
    its second argument.



    HTH,

    Charles K. Clarkson
    --
    Head Bottle Washer,
    Clarkson Energy Homes, Inc.
    Mobile Home Specialists
    254 968-8328

    Charles K. Clarkson Guest

  5. #4

    Default RE: Two-liner to one-liner


    Just a small correction: In scalar context it returns 1 because there
    is 1 element in the list. If there were more parentheses it might
    return a higher number.



    -----Original Message-----
    From: Charles K. Clarkson [mailto:cclarkson@htcomp.net]
    Sent: Wednesday, January 28, 2004 6:13 PM
    To: 'Jan Eden'; 'Perl Lists'
    Subject: RE: Two-liner to one-liner

    I think(?) "( $eingabe =~ /(?<=Teilnehmer:\n\n)(.+)/s )[0]"
    is forcing the regex into list context. In scalar context it returns 1
    (for success?) and split assumes scalar context of its second argument.

    Tim Johnson Guest

  6. #5

    Default Re: Two-liner to one-liner

    On Jan 27, Jan Eden said:
    >my($teilnehmer) = $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;
    As far as I can tell, there is no need for a look-behind in this regex.

    my ($teilnehmer) = $eingabe =~ /Teilnehmer:\n\n(.+)/s;

    should be sufficient.
    >my(@teilzeilen) = split /\n/, $teilnehmer;
    As for shrinking them

    my @teilzeilen = split /\n/, ($eingabe =~ /Teilnehmer:\n\n(.+)/s)[0];

    should work.

    --
    Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  7. #6

    Default RE: Two-liner to one-liner

    Thanks for all the suggestions. This is a very helpful list.

    Charles K. Clarkson wrote:
    >Jan Eden <lists@jan-eden.de> wrote:
    >:
    >: I tried to stuff the following two lines into one, but did
    >: not succeed:
    >:
    >: my($teilnehmer) = $eingabe =~ m/(?<=Teilnehmer:\n\n)(.+)/s;
    >: my(@teilzeilen) = split /\n/, $teilnehmer;
    >
    > Care to share a typical value for $eingabe with us?
    >
    It's just a bunch of text. The string "Teilnehmer:\n\n" appears just beforethe list of attendants (which I want to extract).
    > How about:
    >
    >my @teilzeilen = split /\n/, ( $eingabe =~ /(?<=Teilnehmer:\n\n)(.+)/s )[0];
    >
    > I think(?) "( $eingabe =~ /(?<=Teilnehmer:\n\n)(.+)/s )[0]"
    >is forcing the regex into list context. In scalar context it
    >returns 1 (for success?) and split assumes scalar context of
    >its second argument.
    >
    Ah! I knew about regex results in scalar and list context, but I took the @teilzeilen to define a list context, while in fact split defines the context. My fault.

    BTW, accessing $1 like this implies that $1, $2 ... form an array. How can it be accessed as a whole?

    Thanks again,

    Jan
    --
    How many Microsoft engineers does it take to screw in a lightbulb? None. They just redefine "dark" as the new standard.
    Jan Eden Guest

  8. #7

    Default RE: Two-liner to one-liner

    Since 0 is always false and all other numbers are true, the returned numberof elements can indicate success at the same time. My admiration for the the way Larry Wall thinks is growing by the minute.

    - Jan

    Tim Johnson wrote:
    >
    >Just a small correction: In scalar context it returns 1 because there
    >is 1 element in the list. If there were more parentheses it might
    >return a higher number.
    >
    >
    >
    >-----Original Message-----
    >From: Charles K. Clarkson [mailto:cclarkson@htcomp.net]
    >Sent: Wednesday, January 28, 2004 6:13 PM
    >To: 'Jan Eden'; 'Perl Lists'
    >Subject: RE: Two-liner to one-liner
    >
    > I think(?) "( $eingabe =~ /(?<=Teilnehmer:\n\n)(.+)/s )[0]"
    >is forcing the regex into list context. In scalar context it returns 1
    >(for success?) and split assumes scalar context of its second argument.
    >
    >
    --
    There are 10 kinds of people: those who understand binary, and those who don't
    Jan Eden Guest

  9. #8

    Default Re: Two-liner to one-liner

    Jan Eden wrote:
    >
    > BTW, accessing $1 like this implies that $1, $2 ... form an array. How can it be accessed as a whole?
    my @matches = /($regex)/g;
    my @matches = /hard-coded random stuff(.*) boilerplate(.*)more unwanted (.*)/;

    Joseph

    R. Joseph Newton Guest

  10. #9

    Default Re: Two-liner to one-liner

    "R. Joseph Newton" wrote:
    Oooops, sorry, needs a small adjustment, I think:
    > Jan Eden wrote:
    >
    > >
    > > BTW, accessing $1 like this implies that $1, $2 ... form an array. How can it be accessed as a whole?
    >
    > my @matches =( /($regex)/g);
    > my @matches = (/hard-coded random stuff(.*) boilerplate(.*)more unwanted (.*)/);
    To put the regex expressions into list context.
    > Joseph


    R. Joseph Newton Guest

  11. #10

    Default Re: Two-liner to one-liner

    R. Joseph Newton wrote:
    >"R. Joseph Newton" wrote: Oooops, sorry, needs a small adjustment, I
    >think:
    >
    >>Jan Eden wrote:
    >>
    >>>
    >>>BTW, accessing $1 like this implies that $1, $2 ... form an array.
    >>>How can it be accessed as a whole?
    >>
    >>my @matches =( /($regex)/g);
    Ok, that should have been obvious. Thank you.
    >To put the regex expressions into list context.
    >
    Doesn't the array on the left already form a list context? Do I really needthe parentheses around the regex part?

    - Jan
    --
    There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson
    Jan Eden Guest

  12. #11

    Default Re: Two-liner to one-liner

    Jan Eden wrote:
    >
    > R. Joseph Newton wrote:
    >
    > >"R. Joseph Newton" wrote: Oooops, sorry, needs a small adjustment, I
    > >think:
    > >
    > >>Jan Eden wrote:
    > >>
    > >>>
    > >>>BTW, accessing $1 like this implies that $1, $2 ... form an array.
    > >>>How can it be accessed as a whole?
    > >>
    > >>my @matches =( /($regex)/g);
    >
    > Ok, that should have been obvious. Thank you.
    >
    > >To put the regex expressions into list context.
    > >
    > Doesn't the array on the left already form a list context?
    Yes.
    > Do I really need the parentheses around the regex part?
    No, no parenthesis are required.

    my @matches = /$regex/g;


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139