Parsing Perl.. Please explain..

Discussion in 'Perl Misc' started by Oldbitcollector, Jun 7, 2006.

  1. I've got a routine that looks something like this...

    for( $i = 1; $i <= $pop->Count(); $i++ ) {
    foreach( $pop->Head( $i ) ) {
    /^(From|Subject):\s+/i && print $_, "\n";
    }
    }

    This parses two lines from an email header. (From & Subject)

    I'm having some trouble understanding how this works.
    I'm shooting for a parse that would put this lines into variables as
    well as print them.

    something like

    $from = /^(From):\s+/i
    print $from;

    $subject = /^(Subject):\s+/i
    print $subject;

    Could someone take a little time to explain how this works so I can
    apply it correctly to my own code requirements. Thanks

    Jeff
     
    Oldbitcollector, Jun 7, 2006
    #1
    1. Advertising

  2. Oldbitcollector <> wrote:


    > I've got a routine that looks something like this...
    >
    > for( $i = 1; $i <= $pop->Count(); $i++ ) {



    foreach my $i ( 1 .. $pop->Count() ) {


    > foreach( $pop->Head( $i ) ) {
    > /^(From|Subject):\s+/i && print $_, "\n";
    > }
    > }
    >
    > This parses two lines from an email header. (From & Subject)
    >
    > I'm having some trouble understanding how this works.



    When you have things "and"ed together, as soon as you encounter
    a false value, you can stop evaluating clauses because you already
    know that the whole expression must be false.

    Perl takes that same short cut.

    When the m// is false, the print() is never evaluated.

    The print() is evaluated only when the m// is true.

    The body of the for loop is equivalent to:

    if ( /^(From|Subject):\s+/i ) {
    print $_, "\n";
    }


    > I'm shooting for a parse that would put this lines into variables as
    > well as print them.



    if ( /^(From|Subject):\s+/i ) {
    print $_, "\n";
    $hash{$1} = $_;
    }


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 7, 2006
    #2
    1. Advertising

  3. Oldbitcollector

    Joe Smith Guest

    Oldbitcollector wrote:

    > something like
    >
    > $from = /^(From):\s+/i
    > print $from;
    >
    > $subject = /^(Subject):\s+/i
    > print $subject;


    my($from,$subject);
    while(...) {
    /^From:\s+/i and $from = $_;
    /^Subject:\s+/i and $subject = $_;
    }
    print $from if defined $from;
    print $subject if defined $subject;

    A better way would be to use a hash instead of separate values.
    You can use

    /^(From|Subject):\s/i and $requested_headers{lc $1} = $_;

    or

    $requested_headers{lc $1} = $_ if /^(From|Subject):\s/i;

    to do that.

    Both statements do the same thing; it's a matter of style whether you prefer
    (condition) and $variable = $value;
    versus
    $variable = $value if (condition);

    -Joe
     
    Joe Smith, Jun 7, 2006
    #3
  4. Thanks guys,

    With that explaination and a couple more hours of study, I'm starting
    to understand how this works.


    Jeff
     
    Oldbitcollector, Jun 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    914
    GIMME
    Feb 11, 2004
  2. KK
    Replies:
    2
    Views:
    664
    Big Brian
    Oct 14, 2003
  3. Kaye Ng
    Replies:
    8
    Views:
    188
    Josh Cheek
    Jun 8, 2010
  4. albatroz
    Replies:
    1
    Views:
    168
    Tad McClellan
    Aug 31, 2004
  5. doolittle
    Replies:
    8
    Views:
    89
    -berlin.de
    Jan 16, 2007
Loading...

Share This Page