A little note on efficiency.

First create a file (win32):

perl -wle"for ('00001'..'10000') { print join ' ', ($_) x 20; }" > + temp.tmp

Then get @temp in various ways and look at peak memory consumption.

open my $fh, 'temp.tmp' or die $!; # Alternative 1: 40 984 K my @temp = map { "$_\n" } map { split ' ' } <$fh>; # Alternative 2: 29 076 K my @temp = map { split ' ' } <$fh>; $_ .= "\n" for @temp; # Alternative 3: 14 816 K local $_; my @temp; push @temp => map "$_\n", split ' ' while <$fh>;

Alternative 2 is less memory hungry (as well as faster) because Perl doesn't have to build up an extra list. Alternative 3 is even less memory hungry because it doesn't slurp the file before building up @temp. Of course, there could've been a fourth alternativ having alternativ 2's for loop, but I think the point is made already.

(I use ActivePerl v5.8.0 built for MSWin32-x86-multi-thread, build 806.)

Hope this helps,
ihb


In reply to Re: using map and anonymous subroutines by ihb
in thread using map and anonymous subroutines by ritontor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.