Home > Archive > PERL Beginners > July 2007 > Re: Temporary variables with map { }
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: Temporary variables with map { }
|
|
| Chas Owens 2007-07-15, 6:59 pm |
| On 7/15/07, Rodrick Brown <rodrick.brown@gmail.com> wrote:
> @list = map { $c = $_; $c =~ s/\s+/_/g; $c } @d = grep { /\d/ } <FH>;
>
> Is there a way to shorten the following mostly concerned with why I
> have to use a temporary variable.
Unless you have some reason to store the intermediate results you can say:
@list = map { s/\s+/_/g; $_ } grep { /\d/ } <FH>;
But this is probably a bad idea; generally speaking, file handles
should not be put in list context.
| |
| Jeff Pang 2007-07-15, 9:58 pm |
| > generally speaking,file handles should not be put in
list context.
Sorry that didn't hear this.Why should not?
________________________________________
________________________________________
____
Get the free Yahoo! toolbar and rest assured with the added security of spyware protection.
http://new.toolbar.yahoo.com/toolba...orton/index.php
| |
| Chas Owens 2007-07-15, 9:58 pm |
| On 7/15/07, Jeff Pang <jeff.pang@yahoo.com> wrote:
> list context.
>
> Sorry that didn't hear this.Why should not?
Because you slurp the entire contents of the file into memory. That
may work fine now, but what happens when the file is larger?
| |
| Uri Guttman 2007-07-15, 9:58 pm |
| >>>>> "CO" == Chas Owens <chas.owens@gmail.com> writes:
CO> On 7/15/07, Jeff Pang <jeff.pang@yahoo.com> wrote:[color=darkred]
CO> Because you slurp the entire contents of the file into memory. That
CO> may work fine now, but what happens when the file is larger?
and how large is too large? in ye olde days, even 32k was a serious
chunk of ram. now 1mb is nothing in systems with gigabytes. other than
files that are known to be very large such as logs, genetic data,
etc. slurping is not to be feared. and it can be much faster than line
by line in many situations. and slurping into a string makes all sorts
of things possible that you can't as easily do line by line. one example
from a recent thread was handling line continuations. with a file
slurp into a scalar, a single s/// op will merge all continued lines
without any looping or extra logic and it will be much faster as well.
so stop the fud about slurping. 99% of all the files you will see and
read are small enough to slurp. configs, source, content, etc files are
all fine to slurp.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
|
|
|
|
|