Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Pulling out lines of text from a text file

Reply
Thread Tools

Pulling out lines of text from a text file

 
 
Xicheng
Guest
Posts: n/a
 
      03-10-2006
Dr.Ruud wrote:
> Uri Guttman schreef:
>
> > even better:
> >
> > while (<FILE>) {
> > next if $. < $number ;
> > print $_ ;
> > last;
> > }

>
> while (<FILE>) {
> $. == $number or next;
> print;
> last;
> }
>


why not just:

while(<FILE>) {
print and last if $. == $number;
}

Xicheng

> Affijn, Ruud
> "Gewoon is een tijger."


 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      03-10-2006
"Dr.Ruud" <(E-Mail Removed)> wrote:

> Uri Guttman schreef:
>
>> even better:
>>
>> while (<FILE>) {
>> next if $. < $number ;
>> print $_ ;
>> last;
>> }

>
> while (<FILE>) {
> $. == $number or next;
> print;
> last;
> }


Ok I just closed my "Reply" .

The reason why I prefer this one is that I read the $. == $number as what
must be true for the rest to happen. Since I read left to right, I "see"
it faster compared to next if ...

--
John Experienced Perl programmer: http://castleamber.com/
 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      03-10-2006

"Xicheng" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Dr.Ruud wrote:
> > Uri Guttman schreef:
> >
> > > even better:
> > >
> > > while (<FILE>) {
> > > next if $. < $number ;
> > > print $_ ;
> > > last;
> > > }

> >
> > while (<FILE>) {
> > $. == $number or next;
> > print;
> > last;
> > }
> >

>
> why not just:
>
> while(<FILE>) {
> print and last if $. == $number;
> }
>


This is heading towards the providing of a good example of my reservations
about that construct that does away with the curly braces.

I mean - if:

while(<FILE>) {
print $_;
}

can be replaced with:

print $_ while <FILE>

then I expect that:

while(<FILE>) {
print and last if $. == $number;
}

can be replaced with:

print and last if $. == $number while <FILE>;

There's an inconsistency in the implementation of this feature that leaves
me feeling rather cold.

Cheers,
Rob


 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-10-2006
>>>>> "S" == Sisyphus <(E-Mail Removed)> writes:

S> I mean - if:

S> while(<FILE>) {
S> print $_;
S> }

S> can be replaced with:

S> print $_ while <FILE>

S> then I expect that:

S> while(<FILE>) {
S> print and last if $. == $number;
S> }

S> can be replaced with:

S> print and last if $. == $number while <FILE>;

the problem with multiple statement modifier is that they are not clear
in what they mean and are tricky to correctly parse out. larry has
stated that perl5 will never get them but i think it has been discussed
in the perl6 lists.

any i would write that (if i wanted to write code like this which i
don't):

$. == $number and print and last while <FILE>;

or even (and the precedence is correct as , binds before 'and'

$. == $number and print, last while <FILE>;

but i don't like compound statements like that (which is not the same as
a boolean expression which modifies a statement).

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-10-2006
Sisyphus <(E-Mail Removed)> wrote:
> "Xicheng" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...



>> while(<FILE>) {
>> print and last if $. == $number;
>> }
>>

>
> This is heading towards the providing of a good example of my reservations
> about that construct that does away with the curly braces.



The docs (perlsyn) call them "modifiers".


> I mean - if:
>
> while(<FILE>) {
> print $_;
> }
>
> can be replaced with:
>
> print $_ while <FILE>
>
> then I expect that:
>
> while(<FILE>) {
> print and last if $. == $number;
> }
>
> can be replaced with:
>
> print and last if $. == $number while <FILE>;



You shouldn't expect that because it is documented to be disallowed. <g>

perlsyn says you can have only 1 modifier, you are using 2 modifiers.


> There's an inconsistency in the implementation of this feature that leaves
> me feeling rather cold.



Yes, I see that too.

I'm pretty sure that Larry saw that too too.

I remember hearing/reading somewhere that he purposely limited
it to 1 modifier because it would/could lead to some really
hard-to-understand code.

I thought the "somewhere" was in the std docs, and that

grep BASIC *.pod

would find it (because the feature was borrowed from BASIC-PLUS),
but I don't see in there anymore (v5.8.7).



The objection to lack of curly brackets is the "dangling else"
problem in disguise, and so is probably a widely held objection.

Which may be why Larry limited modifiers to only one...?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      03-10-2006
John Bokma:
> Dr.Ruud:
>> Uri Guttman:


>>> even better:
>>>
>>> while (<FILE>) {
>>> next if $. < $number ;
>>> print $_ ;
>>> last;
>>> }

>>
>> while (<FILE>) {
>> $. == $number or next;
>> print;
>> last;
>> }

>
> Ok I just closed my "Reply" .
>
> The reason why I prefer this one is that I read the $. == $number as
> what must be true for the rest to happen. Since I read left to right,
> I "see" it faster compared to next if ...


It is much like what "perl -MO=Deparse,-x7" makes of this:

while (<FILE>) {
next unless $. == $number;
print;
last;
}

------

The "next if $. != $number" version becomes more like:

while (<FILE>) {
$. != $number and next;
print;
last;
}

======

The "$. < $number" version has a problem, if the same block can be used
in situations where $number can be any number.

while (<FILE>) {
die "huh?" if $. > $number;
next if $. < $number;
print;
last;
}

------

If you don't like to use 'next' but have no problem with 'last':

while (<FILE>) {
die "huh?" if $. > $number;
if ($. == $number) {
print;
last;
}
}

------

If you don't like to use 'next' nor 'last':

for (my $last = 0 ; defined($_=<>) and not $last ; ) {
die "huh?" if $. > $number;
if ($. == $number) {
print;
$last = 1;
}
}

(hey, just trying to scare you)

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      03-10-2006

"Tad McClellan" <(E-Mail Removed)> wrote in message
..
..
>
> I remember hearing/reading somewhere that he purposely limited
> it to 1 modifier because it would/could lead to some really
> hard-to-understand code.
>


When I look at the code that uri has just provided, I start to think that
the horse has already bolted on that score ... and that allowing one
modifier is one modifier too many

To save you looking it up, uri presented the following 2 alternatives:

$. == $number and print and last while <FILE>;
$. == $number and print, last while <FILE>;

That's plenty hard enough for *me* to understand

(And, yes - I've also pondered that "dangling else" as you called it ....
didn't realize it had a name.)

Cheers,
Rob



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-10-2006
Sisyphus <(E-Mail Removed)> wrote:

> (And, yes - I've also pondered that "dangling else" as you called it ....
> didn't realize it had a name.)



It is a common topic in computer science, google it.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
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
FAQ 6.3 How can I pull out lines between two patterns that are themselves on different lines? PerlFAQ Server Perl Misc 0 04-19-2011 10:00 AM
FAQ 6.3 How can I pull out lines between two patterns that are themselves on different lines? PerlFAQ Server Perl Misc 0 01-14-2011 05:00 AM
How to filter out lines from a variable that has multi-lines? mike Perl Misc 3 10-02-2007 12:08 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
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments