in reply to Re: Array of arrays
in thread Array of arrays

I agree with tobyink, except that recreating the arrays when entering the second routine is not really optimal:

sub secondRoutine { my @array1 = @{ shift(@_) }; my @array2 = @{ shift(@_) }; my @array3 = @{ shift(@_) }; print ("Test first value in array1: $array1[0]"); print ("Test third value in array2: $array2[2]"); print ("Test fifth value in array3: $array3[4]"); }

I would usually prefer something like this:

<c>sub secondRoutine { my ($array1_ref, $array2_ref, $array3_ref) = @_; print ("Test first value in array1: $$array1_ref[0]"); print ("Test third value in array2: $$array2_ref[2]"); print ("Test fifth value in array3: $$array3_ref[4]"); }

This is matter open to discussion. Sometimes, I also create intermediary arrays from arrayrefs or hashref to make sure that I am really understanding my complicated data structure, but, here, it seems quite simple, no point of using memory to copy data structures.

Update: fixed the copy-and-paste error seen by Monk::Thomas.

Replies are listed 'Best First'.
Re^3: Array of arrays
by Monk::Thomas (Friar) on Jul 19, 2013 at 08:07 UTC
    (Copy&Paste error in your 'sub secondRoutine'? You define $array1_ref, $array2_ref, $array3_ref but use $$array1[0])
    sub secondRoutine { # there's nothing wrong here, but $aref (and $href) are shorter and # convey the same meaning #my ($array1_ref, $array2_ref, $array3_ref) = @_; my ($aref1, $aref2, $aref3) = @_; # - use references directly # - added newlines for clearer output # - brackets are unnecessary (just a style choice, no actual impact) #print ("Test first value in array1: $$array1[0]"); #print ("Test third value in array2: $$array2[2]"); #print ("Test fifth value in array3: $$array3[4]"); print "Test first value in array1: ", $aref1->[0], "\n"; print "Test third value in array2: ", $aref2->[2], "\n"; print "Test fifth value in array3: ", $aref3->[4], "\n"; }