Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Parsing Perl.. Please explain..

Reply
Thread Tools

Parsing Perl.. Please explain..

 
 
Oldbitcollector
Guest
Posts: n/a
 
      06-07-2006
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

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      06-07-2006
Oldbitcollector <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      06-07-2006
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
 
Reply With Quote
 
Oldbitcollector
Guest
Posts: n/a
 
      06-07-2006
Thanks guys,

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


Jeff

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 08:58 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments