Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Alternate ways to loop over input (was: I think this is a simple one...)

by mjn (Acolyte)
on Mar 18, 2003 at 15:51 UTC ( [id://244038]=perlquestion: print w/replies, xml ) Need Help??

mjn has asked for the wisdom of the Perl Monks concerning the following question:

But I am still doing something stupid here. My question is: is there a faster/smarter way to do something like this:
while (defined($current_line = $input->getline())) { next if $current_line !~ /^\w+-owner:/; print "$current_line\n"; }
I have tried a few different permutations of this which all seem slow and this one, while pretty quick, seems to loop. The file I am opening and reading is a sendmail aliases file which contains ~4300 lines and I am only interested in those which begin with something like:

listname-owner:     bob@hostname.com

Any input would be much appretiated...
_____________________
mjn

Edited 2003-03-19 by mirod: changed the title

Replies are listed 'Best First'.
Re: I think this is a simple one...
by zby (Vicar) on Mar 18, 2003 at 15:57 UTC
    while(<$input>){ print "$_\n" if /^\w+-owner:/; }
    And look for the <> operator in the manual.

    Update: Shame on me to leave that line with next in the first version. Now it is a lot more conscise.

Re: I think this is a simple one...
by CukiMnstr (Deacon) on Mar 18, 2003 at 16:06 UTC
    you could do something like:
    while ($input->getline()) { print if m/^\w+-owner:/; # print $_, "\n" if m/^\w+-owner:/; }

    I don't see why your code would loop, maybe there is something wrong with $input->getline()?

    hope this helps,

Re: I think this is a simple one...
by mjn (Acolyte) on Mar 18, 2003 at 17:25 UTC
    Ok. I've been a good little perlmonger and started using the spaceship to play with my input instead of that other mess I started out with. The loop that i perceived was as a result of a foreach loop i had incorporated as a part of a previous iteration of this. Removing it stopped the stpidness. Thanks for all of the help.
    _____________________
    mjn
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://244038]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-29 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found