Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

2D arrays

by Anonymous Monk
on Jul 02, 2012 at 19:59 UTC ( #979529=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to take a list (@list) and split it up into an array of 38 arrays each contains 38 values from my @list, they are already in order it just has to step through and get the data. moving it from @list to my 2d array @temp as it goes here is the code for the sub i am using

use strict; use warnings; sub sortList{ my @temp ; my (@list) = @_; my $j; my $count = 0; for (my $i =0; $i< 38; $i += 1){ for ( $j=0; $j<38; $j+= 1){ push (@{$temp[$i]}, $list[$count]); $count += 1; } } return @temp; }

rather than getting the values in my my output i am getting a list of array reference numbers not sure why. This is my first time using a 2D array any wisdom would be appreciated

Replies are listed 'Best First'.
Re: 2D arrays
by davido (Archbishop) on Jul 02, 2012 at 20:50 UTC

    Perl's 2d arrays are actually arrays of array references. Each array reference, when dereferenced will contain 38 elements. perllol has a full description of manipulating "lists of lists". And perlreftut as well as perlref will get you started on dealing with references.

    You might inspect the output like this:

    my @output = sortList( @flatlist ); foreach my $array_ref ( @output ) { print "@{$array_ref}\n"; }

    Another way to write your "38 at a time" code is to use List::MoreUtils natatime function, like this:

    use strict; use warnings; use List::MoreUtils qw( natatime ); my @biglist = (1 .. 1444); foreach my $array_ref ( sortList( @biglist ) ) { print "@{$array_ref}\n"; } sub sortList { my $it = natatime 38, @_; my @output; while ( my @sublist = $it->() ) { push @output, \@sublist; } return @output; }


Re: 2D arrays
by toolic (Bishop) on Jul 02, 2012 at 21:02 UTC
    From perldsc:
    The most important thing to understand about all data structures in Perl--including multidimensional arrays--is that even though they might appear otherwise, Perl @ARRAY s and %HASH es are all internally one-dimensional. They can hold only scalar values (meaning a string, number, or a reference). They cannot directly contain other arrays or hashes, but instead contain references to other arrays or hashes.

      see this is the part i was not getting i didnt realize that the parent array only held the references of the sub arrays, not the whole array its self

Re: 2D arrays
by AnomalousMonk (Chancellor) on Jul 02, 2012 at 23:20 UTC

    Another problem with the originally posted code is
        push (@{$temp[$i]}, $list[$count]);
            $count += 1;
    because  $count just keeps getting bigger and bigger. Try putting a  print $count, "\n"; statement just before the  return @temp; statement in the original  sortList() function definition: is there anything meaningful in  $list[$count] or anywhere near this index?

    Another approach is to use the Data::Dump module or one of its ilk and put a dump statement after a call to  sortList() to see what you have:
        use Data::Dump;
        my @ra = sortList(0..37);
        dd \@ra;

Re: 2D arrays
by TJPride (Pilgrim) on Jul 03, 2012 at 02:43 UTC
    use strict; use warnings; use Data::Dumper; my (@arr1, @arr2); ### Create array of 1444 elements push @arr1, $_ for 1..38**2; ### Create AoA of 38x38 push @arr2, [ @arr1[($_*38)..($_*38+37)] ] for 0..37; print Dumper(\@arr2);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://979529]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2017-09-24 13:18 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (274 votes). Check out past polls.