I agree that it's easier to use references throughout your program, rather than having to deal with real arrays and references to arrays. The same applies to hashes. One other thing is that you can use the arrow operator to index into the array refererence with square brackets, as the example below demonstrates. You can do a similar thing with hash references.
#!/usr/bin/perl use strict; use warnings; run(); sub run { my $arr1 = [0 .. 12]; my $arr2 = ['a' .. 'l']; print "before:\n"; print "arr1 = @{$arr1}\n"; print "arr2 = @{$arr2}\n"; # because we're passing references to arrays, any changes # made to the arrays in the subroutine will affect the arrays # here as well. No need to return the arrays. f($arr1, $arr2); # use the arrow operator and square brackets to index the # array, same as in the subroutine below - same syntax, # less confusing $arr1->[7] = "SEVEN!"; print "after:\n"; print "arr1 = @{$arr1}\n"; print "arr2 = @{$arr2}\n"; } sub f { my ($arr1, $arr2) = @_; for (my $i = 0; $i < 5; $i++) { # shuffle some stuff around my $index1 = rand($#$arr1); my $index2 = rand($#$arr2); # use the arrow operator and square brackets to index # the array through a reference my $tmp = $arr1->[$index1]; $arr1->[$index1] = $arr2->[$index2]; $arr2->[$index2] = $tmp; } }

In reply to Re^2: Help with references by beable
in thread Help with references by coldmiser

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.