in reply to Array of arrays

This:

push( @array_array, @array1, @array2, @array3);

should probably be:

push( @array_array, \@array1, \@array2, \@array3);

And this:

my (@array1, @array2, @array3) = { shift @_ };

should be:

my @array1 = @{ shift(@_) }; my @array2 = @{ shift(@_) }; my @array3 = @{ shift(@_) };
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Array of arrays
by Laurent_R (Canon) on Jul 18, 2013 at 22:44 UTC
    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.

      (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"; }
Re^2: Array of arrays
by SuzuBell (Acolyte) on Jul 18, 2013 at 23:17 UTC

    Thank you both for the responses. I still get an error (following the advice in the first response). When I do, for instance:

    print ("1st: $array1[0]"); print ("2nd: $array1[1]"); print ("3rd: $array1[2]"); print ("4th: $array1[3]"); print ("5th: $array1[4]"); print ("6th: $array1[5]"); print ("7th: $array1[6]"); print ("8th: $array1[7]"); print ("9th: $array1[8]"); print ("10th: $array1[9]"); print ("11th: $array1[10]"); print ("12th: $array1[11]");
    I get the following error:
    1st: ARRAY(0x7fd4bb012010) 2nd: ARRAY(0x7fd4bb02f768) 3rd: ARRAY(0x7fd4bb0120a0) 4th: ARRAY(0x7fd4bb012028) 5th: ARRAY(0x7fd4bb012070) 6th: ARRAY(0x7fd4bb02f648) 7th: ARRAY(0x7fd4bb045e80) 8th: ARRAY(0x7fd4bb0462e8) 9th: 10th: 11th: 12th : Use of uninitialized value $array1[8] in concatenation (.) or string Use of uninitialized value $array1[9] in concatenation (.) or string Use of uninitialized value $array1[10] in concatenation (.) or string Use of uninitialized value $array1[11] in concatenation (.) or string
    The strange thing is I have 9 arrays (array1, array2, ..., array9) in my array of arrays. But even when I am only calling array1 (which should have ~200 indices), it seems to be considering each array in the array of arrays as simply an index in the first of its arrays??

      The 'ARRAY(0x7fd4bb012010)' lines are telling you that you are attempting to print a Reference to an array. So, to print the 1st array, do this:

      my @first_array = @{$array1[0]}; print @first_array;

      Or maybe better, to print all of the arrays:

      foreach my $array_ref (@array1) { print @{$array_ref}; }

      The remaining errors for 'Use of uninitialized value' are telling you that those parts of the parent array are not defined, and therefore cannot be printed (you would have to see the internal of perlfunc's print for the explanation behind the concatenation part...).

      David

      SuzuBell,
      If you are still having problems fixing your data structure to properly print your values out, use Data::Dumper module to see how your data structure is formed.
      Check what you are printing again and compare it with the previous solutions given.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

      You have been given several suggestions of correction. We don't know what you have implemented and what not. Please show us the full code so that we know what you have now. Or show the output of a Data::Dumper dump command. Better yet, show us both.