in reply to Help with references

I wont reiterate the importance of 'strict' and 'warnings' ... (it is very useful) instead, I'm gonna tell you what you won't hear from anyone else.

I post this because you said this is your first programming language. Others will disagree with what I am about to tell you, and this may seem like weird advice, but you can judge for yourself. ... Disclaimer: extremely biased opinion follows ... IMHO the single best way to 'wrap your brain' around working with references is to absolutely avoid using the 'backslash notation' whenever possible, and just treat everything as a scalar. This may take a moment to sink in, but let me explain, and consider this example ...

### re-worked version of your original code my $ref1 = [(0..9)]; ### start with anon array refs my $ref2 = [('a'..'l')]; ### from the very beginning my ($aOne, $aTwo) = &f($ref1, $ref2); print '@a1 is: '. "@{$aOne}\n"; print '@a2 is: '. "@{$aTwo}\n"; sub f { my $a1 = shift || die 'Hey! I expected an array ref '; my $a2 = shift || die 'expected array ref here too '; ### do something, for example ... @{$a2} = reverse @{$a2}; return ($a1, $a2); } __END__ @a1 is: 0 1 2 3 4 5 6 7 8 9 @a2 is: l k j i h g f e d c b a

rationale

Just remember: 1) "scalars can hold anything" (the variables with the dollar sign in front) use them; 2) give those scalars a good name to reflect what's expected of them; and 3)properly 'morph' those scalars into proper versions of what they are holding with @{$foo}, @$foo, %{$foo}, or %$foo, notation .. or $foo->(), $foo->{baz}{blee}{blaa}( or whatever else is appropriate ) and you're done.

Replies are listed 'Best First'.
Re^2: Help with references
by beable (Friar) on Jul 02, 2004 at 02:24 UTC
    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; } }