Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > extract range of lines using range op bug?

Reply
Thread Tools

extract range of lines using range op bug?

 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      03-03-2006
i was looking in the Cookbook (2nd ed. pg. 199), and tried to extract a
range of lines using the examples given. one uses '..' (inclusive) and
the other uses '...' (exclusive...supposedly).

it appears that they both do the same thing however. for 3dot, i expect
lines between the patterns. for 2dot, i expect lines between and
including the patterns. i get lines between and including for both
however. can anyone explain this? or am i misinterpreting something?

here is the code:
use strict; use warnings;

my $file = 'data/regex_ranges.txt';
open my $fh, '<', $file or die "can't open $file: $!\n";
while ( <$fh> ) {
chomp;
if ( /START/ ... /END/ ) {
print "3dot: >>$_<<\n";
}
}
close $fh;

print "\n", '-' x 40, "\n";

open my $fh2, '<', $file or die "can't open $file: $!\n";
while ( <$fh2> ) {
chomp;
if ( /START/ .. /END/ ) {
print "2dot: >>$_<<\n";
}
}
close $fh2;


# i used files, b/c with __DATA__ the first while used it up, but the
file contains
# the same thing as __DATA__

__DATA__
START test
first name=Homer
middle name=Jay
last name=Simpson
END test
START test
first name=Bart
middle name=
last name=Simpson
END test
START test
first name=Lisa
last name=Simpson
END test

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      03-03-2006
it_says_BALLS_on_your forehead wrote:
> i was looking in the Cookbook (2nd ed. pg. 199), and tried to extract a
> range of lines using the examples given. one uses '..' (inclusive) and
> the other uses '...' (exclusive...supposedly).
>
> it appears that they both do the same thing however. for 3dot, i expect
> lines between the patterns. for 2dot, i expect lines between and
> including the patterns. i get lines between and including for both
> however. can anyone explain this? or am i misinterpreting something?


I don't have my copy of the Cookbook on it, so I don't know the exact
explanation it gives, but the terms 'inclusive' and 'exclusive' sound
suspicious to me. Here's how perldoc perlop defines the difference
between the two:

[the .. operator] is false as long as its left operand is false.
Once the left operand is true, the range operator stays true
until the right operand is true, AFTER which the range
operator becomes false again. It doesn't become false till
the next time the range operator is evaluated. It can test
the right operand and become false on the same evaluation it
became true (as in awk), but it still returns true once. If
you don't want it to test the right operand till the next
evaluation, as in sed, just use three dots ("...") instead
of two. In all other regards, "..." behaves just like ".."
does.


So the only difference between .. and ... is that .. checks the right
operand on the same iteration that the left operand became true. The
.... operator ignores the right operand until the following iteration.

Example:
#!/usr/bin/perl
use strict;
use warnings;

my @lines = <DATA>;
my $num = 0;
for (@lines) {
$num++;
chomp ;
print "Line $num: '$_'\n" if /START/ .. /END/;
}

print "First loop done\n";

$num = 0;
for (@lines){
$num++;
chomp;
print "Line $num: '$_'\n" if /START/ ... /END/;
}


__DATA__
some stuff
the STARTing line
more stuff
the ENDing line
more more stuff
the next STARTing line - that also ENDs
and final more stuff
that will only be printed for ...
until it ENDs


Line 2: 'the STARTing line'
Line 3: 'more stuff'
Line 4: 'the ENDing line'
Line 6: 'the next STARTing line - that also ENDs'
First loop done
Line 2: 'the STARTing line'
Line 3: 'more stuff'
Line 4: 'the ENDing line'
Line 6: 'the next STARTing line - that also ENDs'
Line 7: 'and final more stuff'
Line 8: 'that will only be printed for ...'
Line 9: 'until it ENDs'

In the first one, Line 6 caused the operator to become true and then
immediately false, because both the left and right were true. In the
second one, line six caused the operator to become true, but it did not
become false again until line 9.

Hope that clears it up.

Paul Lalli

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      03-03-2006

it_says_BALLS_on_your forehead wrote:
> i was looking in the Cookbook (2nd ed. pg. 199), and tried to extract a
> range of lines using the examples given. one uses '..' (inclusive) and
> the other uses '...' (exclusive...supposedly).
>
> it appears that they both do the same thing however. for 3dot, i expect
> lines between the patterns. for 2dot, i expect lines between and
> including the patterns. i get lines between and including for both
> however. can anyone explain this? or am i misinterpreting something?
>
> here is the code:
> use strict; use warnings;
>
> my $file = 'data/regex_ranges.txt';
> open my $fh, '<', $file or die "can't open $file: $!\n";
> while ( <$fh> ) {
> chomp;
> if ( /START/ ... /END/ ) {
> print "3dot: >>$_<<\n";
> }
> }
> close $fh;
>
> print "\n", '-' x 40, "\n";
>
> open my $fh2, '<', $file or die "can't open $file: $!\n";
> while ( <$fh2> ) {
> chomp;
> if ( /START/ .. /END/ ) {
> print "2dot: >>$_<<\n";
> }
> }
> close $fh2;
>
>
> # i used files, b/c with __DATA__ the first while used it up, but the
> file contains
> # the same thing as __DATA__
>
> __DATA__
> START test
> first name=Homer
> middle name=Jay
> last name=Simpson
> END test
> START test
> first name=Bart
> middle name=
> last name=Simpson
> END test
> START test
> first name=Lisa
> last name=Simpson
> END test



nvm, it's my misinterpretation. if END appears on the same line as
START, then the 2dot will not proceed to the next line, while 3dot
will. that's the difference. 3dot never tries to test both operators on
the same line., as it says in the book...

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      03-03-2006

Paul Lalli wrote:
> it_says_BALLS_on_your forehead wrote:
> > i was looking in the Cookbook (2nd ed. pg. 199), and tried to extract a
> > range of lines using the examples given. one uses '..' (inclusive) and
> > the other uses '...' (exclusive...supposedly).
> >
> > it appears that they both do the same thing however. for 3dot, i expect
> > lines between the patterns. for 2dot, i expect lines between and
> > including the patterns. i get lines between and including for both
> > however. can anyone explain this? or am i misinterpreting something?

>
> I don't have my copy of the Cookbook on it, so I don't know the exact
> explanation it gives, but the terms 'inclusive' and 'exclusive' sound
> suspicious to me.


you are correct. the book does use the term 'inclusive', but i dragged
it kicking and screaming out of its context, to my own chagrin when i
discovered my error. and then i supplied the word 'exclusive' on my
own. which exacerbated my embarrassment.

> Here's how perldoc perlop defines the difference
> between the two:
>
> [the .. operator] is false as long as its left operand is false.
> Once the left operand is true, the range operator stays true
> until the right operand is true, AFTER which the range
> operator becomes false again. It doesn't become false till
> the next time the range operator is evaluated. It can test
> the right operand and become false on the same evaluation it
> became true (as in awk), but it still returns true once. If
> you don't want it to test the right operand till the next
> evaluation, as in sed, just use three dots ("...") instead
> of two. In all other regards, "..." behaves just like ".."
> does.
>
>
> So the only difference between .. and ... is that .. checks the right
> operand on the same iteration that the left operand became true. The
> ... operator ignores the right operand until the following iteration.



yup, that's what i discovered, thx Paul .

 
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
Extract lines with regular expressions Michael Dore Ruby 2 04-21-2008 08:54 AM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
extract consecutive lines of data baptiste Auguié Ruby 8 11-25-2007 03:46 PM
Extract range of lines from a text file Amer Neely Perl Misc 29 04-10-2006 12:28 AM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM



Advertisments