Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: foreach type of deal?

by kennethk (Abbot)
on Mar 15, 2017 at 18:32 UTC ( #1184743=note: print w/replies, xml ) Need Help??


in reply to Re: foreach type of deal?
in thread foreach type of deal?

Since 500k+ is a big number and the spec says "They all start out with a g", I would rather use substr since there is no need for a test.
while (<>) { my $word = substr($_,1); # Start with the second letter }

As a note for the OP, while is a nicer option than Foreach Loops in this scenario since, in general, foreach constructs the entire list before incrementing over it, whereas while will just hit your file-handle once per loop -- normally, that means while has a much smaller memory footprint than foreach does for large sets. I don't recall if in the particular case of foreach(<$fh>) { there is an optimization in perl to avoid this pitfall.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^3: foreach type of deal?
by BillKSmith (Vicar) on Mar 15, 2017 at 23:07 UTC
    Do we ever have perfect data? I prefer to use a test to warn about invalid data. The following code (untested) makes use of runtime switches (ref: perldoc perlrun). Use -p for the I/O and -i for backup.
    #!perl -p -ibak if (!s/^g(Ord_\d{4}\.png)\s*$/$1/) { warn "Invalid line: $_\n"; $_ = <>; redo; }
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1184743]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2017-10-21 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (269 votes). Check out past polls.

    Notices?