A "C" 1D "array" could be called in Perl as a "list". But a Perl list is very different and much more powerful than a "C" 1-Dimensional array. Just one part of this is that there is no need to keep track of the size! There are many aspects of this which are beyond the scope of this post.

Perl is also different in many other respects to other languages. A variable like @array can have a list context as well as a scalar context. If you had written my $array = @array, $array would have been the number of elements in the list @array. In Perl, each type of variable has its own namespace, so $list=@list is completely fine.

To take a reference to a list, use: my listRef = \@list. This is "sort of" like an address of an array in C.

In the below code, I send a reference to a list into a sub. @$listRef expands that reference back into the original list.

One of the really magic things about Perl is that it is almost NEVER necessary to use something like: for ( my $i=1; $i<=$size; $i++ ). There are situations for this, but only perhaps once per 5K+ lines of Perl code - this is a rare duck! I did print an "index" in one situation below, but note that it is NOT the looping variable!

#!/usr/bin/perl -w use strict; my @list= (1,2,3,4,5); printListRef(\@list); sub printListRef { my $listRef = shift; ########### easy way ######## print "easy way\n"; print "@$listRef\n"; ############################# #### another way (ugly)########### print "\nugly index stuff way\n"; my $i =0; foreach my $item (@$listRef) { print "list element[$i]=$item\n"; $i++; } ########## perl way for item per line ### print "\nperl way with one item per line\n"; print join("\n",@$listRef),"\n"; } ## it might not be apparent, but the __END__ ##line stopped the compilation. Another cool this about Perl. __END__ prints: easy way 1 2 3 4 5 ugly index stuff way list element[0]=1 list element[1]=2 list element[2]=3 list element[3]=4 list element[4]=5 perl way with one item per line 1 2 3 4 5

In reply to Re: reference array and pass ref to sub by Marshall
in thread reference array and pass ref to sub by csarid

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.