As I understand it.. Subroutines do not return arrays or hashes.
They can return one zero or more values, that at the receiving end can be sown together into an array/hash/whatever, making it seem like you returned an array or hash.

Some example code (untested.)..

my @array1 = ('Paula', 'Sherri', 'Cynthia', 'Ludovico'); my @gotback = names_a(); $gotback[0] = 'Jimmy'; # @array1 is still ('Paula', 'Sherri', 'Cynthia', 'Ludovico') my $gotback = name_aref(); $gotback->[0] = 'Jimmy'; # @array1 is now ('Jimmy', 'Sherri', 'Cynthia', 'Ludovico') my @gotback2 = @{ names_aref() }; # makes a copy $gotback2[0] = 'Back to Paula'; # @array1 is still ('Jimmy', 'Sherri', 'Cynthia', 'Ludovico') # because we made a copy by doing @{ names_aref() } sub names_a { # this really returns # 'Jimmy', 'Sherri', 'Cynthia','Ludovico' # and not @array1 # when received at the other end, it may be slapped # back togeter into an array, # but it will be a new list (a new array) return @array1; } sub names_aref { # this returns a reference, therefore both what this is # returning and the original @array1 both point to the # same space in the symbol table (place in memory). return \@array1; }

In reply to Re: Best way to return array or hash? by leocharre
in thread Best way to return array or hash? by Anonymous Monk

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.