Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Newbie question - trying to get a handle on until text / line skipping

Reply
Thread Tools

Newbie question - trying to get a handle on until text / line skipping

 
 
Chris Vidal
Guest
Posts: n/a
 
      07-15-2003
Lets say I have a ascii text file named sumfil and its contents are as
follows :

1
2
3
a
b
c

I dont want to act on any line before the line containing "a". The below
doesnt work. I sit at the command line I think in an infinite loop.

#! /opt/perl5/bin/perl
$tfil = "sumfil";
open (IN, "$tfil") or die;
while (defined($line = <IN>)) {
until ($line =~ "3") {
next;
} # end until
chomp $line;
print "$line\n"
} #end while





 
Reply With Quote
 
 
 
 
nobull@mail.com
Guest
Posts: n/a
 
      07-16-2003
"Chris Vidal" <(E-Mail Removed)> wrote in message news:<bf1f24$(E-Mail Removed)>...
> Lets say I have a ascii text file named sumfil and its contents are as
> follows :
>
> 1
> 2
> 3
> a
> b
> c
>
> I dont want to act on any line before the line containing "a". The below
> doesnt work. I sit at the command line I think in an infinite loop.
>
> #! /opt/perl5/bin/perl
> $tfil = "sumfil";
> open (IN, "$tfil") or die;
> while (defined($line = <IN>)) {
> until ($line =~ "3") {
> next;
> } # end until
> chomp $line;
> print "$line\n"
> } #end while


You have misunderstood what until means in Perl.

until (EXPR) {BLOCK}

is a _looping_ construct.

_Each_ time the above is executed it will repeatedly do BLOCK until
EXPR is true. If BLOCK cannot make EXPR become false and does not
break out of the loop by some other means then this is an infinite
loop.

You you evidently believe until is a simple negated condition with
memory. i.e. it does something once each time it is executed until
some condition is true and thereafter never does it again.

In Perl that would be written:

unless ( EXPR .. 1 == 0 ) { BLOCK }

Also you want the 'next' to go to the next line, i.e. the next
iteration of the while loop. But by default it goes to the next
iteration of the inner-most looping construct (the until). You could
get around this by labling your loops (see perldoc -f next).

But, of course, you didn't want the inner loop at all.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 
Reply With Quote
 
 
 
 
Chris Vidal
Guest
Posts: n/a
 
      07-17-2003
Yes you are right, thats why I posted my question here.

...cant figure it out or find a good example.

I want to skip some lines ...


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "Chris Vidal" <(E-Mail Removed)> wrote in message

news:<bf1f24$(E-Mail Removed)>...
> > Lets say I have a ascii text file named sumfil and its contents are as
> > follows :
> >
> > 1
> > 2
> > 3
> > a
> > b
> > c
> >
> > I dont want to act on any line before the line containing "a". The below
> > doesnt work. I sit at the command line I think in an infinite loop.
> >
> > #! /opt/perl5/bin/perl
> > $tfil = "sumfil";
> > open (IN, "$tfil") or die;
> > while (defined($line = <IN>)) {
> > until ($line =~ "3") {
> > next;
> > } # end until
> > chomp $line;
> > print "$line\n"
> > } #end while

>
> You have misunderstood what until means in Perl.
>
> until (EXPR) {BLOCK}
>
> is a _looping_ construct.
>
> _Each_ time the above is executed it will repeatedly do BLOCK until
> EXPR is true. If BLOCK cannot make EXPR become false and does not
> break out of the loop by some other means then this is an infinite
> loop.
>
> You you evidently believe until is a simple negated condition with
> memory. i.e. it does something once each time it is executed until
> some condition is true and thereafter never does it again.
>
> In Perl that would be written:
>
> unless ( EXPR .. 1 == 0 ) { BLOCK }
>
> Also you want the 'next' to go to the next line, i.e. the next
> iteration of the while loop. But by default it goes to the next
> iteration of the inner-most looping construct (the until). You could
> get around this by labling your loops (see perldoc -f next).
>
> But, of course, you didn't want the inner loop at all.
>
> This newsgroup does not exist (see FAQ). Please do not start threads
> here.



 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      07-18-2003
"Chris Vidal" <(E-Mail Removed)> rudely spits TOFU in my face:

> Yes you are right,


I said a lot of things. As far as I know all of them were true.
Without context I have no idea which to refer to.

> thats why I posted my question here.


None of the things I said could have accounted for your decision to
post to a non-existant newsgroup.

> ...cant figure it out or find a good example.


I have no idea what you are talking about.

> I want to skip some lines ...


This was clear from your original post. And appart from your
confusion about 'until' and 'unless' you were basically doing it right
(i.e. using 'next') in your OP. Now I've cleared that up your code
would almost work (except it would include in the output the line that
triggered the state change).

The obvious way to avoid that just to spell it out:

use strict;
use warnings;

my $seen_trigger;
while (<>) {
unless ( $seen_trigger ) {
$seen_trigger = /PATTERN/;
next;
}
print;
}

If you still have a problem then I suggest you produce another minimal
but complete script that illustrates your problem (see posting
guidelines in comp.lang.perl.misc) and post it to that newsgroup
because this one does not exist (see FAQ).

If someone tries to help do not insult them by spitting TOFU in their
faces (see guidelines). Doing so will rapidly exhaust your quota of
good-will.
 
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
Parsing a text file line-by-line: skipping badly-formed lines? denis.papathanasiou@gmail.com Perl Misc 27 05-18-2007 07:07 PM
Package to handle table text render (handle space or tab betweenthe columns) ? =?ISO-8859-1?Q?KLEIN_St=E9phane?= Python 3 10-06-2006 08:46 AM
How's JRE handle the process of Applet Destroy (IE6)? trace stopped until "joined applet thread" soldieryap81@yahoo.com Java 0 09-28-2006 09:40 AM
skipping line while reading data file Ivan Liu C++ 3 09-05-2006 02:29 PM
how to handle command line output(not terminal handle) Leon Python 2 11-04-2004 05:16 AM



Advertisments