Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Neither snippet will do what you think it will do. chomp returns whatever is chomped off, not the line, so $a, will end up being a bunch of delimiters.

In terms of performance, there will be little to choose between them; they'll both be quite slow.

In terms of memory, the first will hang on to the memory used by @lines until you explicitly clean it.

You might find this does what you are after more efficiently:

my @lines = foo( 'bar' ); chomp @lines; my $a = join '', @lines; my $b; $b *= getVa( $_ ) for @lines; undef @lines;

As for scaling: if fun() can returns very large numbers of lines, building an array inside the function and then returning that array as a list will scale badly.

You could either pass a reference to the array into foo() and having it populate it; or return a reference to the array constructed internally; this avoiding the construction of the list and the copying of the array.

But better yet would be to move the chomping, concatenation and parsing inside the loop where the lines are read or generated, and return the concatenated string and the accumulated total; thus avoiding the construction of the array completely:

sub foo{ my $bar = shift; my( $a, $b ); ... while( my$line = getLine() ) { chomp $line; $a .= line; $b *= getVa( $line ) } return $a, $b; } my( $a, $b ) = foo( 'bar' );

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: using function output ananymous array vs. named array in foreach by BrowserUk
in thread using function output ananymous array vs. named array in foreach by ISAI student

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (6)
    As of 2018-06-20 15:59 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (116 votes). Check out past polls.