5

I am a Perl newbie and I am currently learning how Perl handles multi-dimensional arrays and how I can access the elements.

A database query is executed in a subroutine, and return the array @stash:

...
while ( $array_ref = $sth->fetchrow_arrayref ) {
 push @sub_stash, [ @$array_ref ];
 }
$dbh->disconnect;
return ( @sub_stash );

@stash is received in the main, and manage to print the contents of the array as follows:

@stash = query($var);
### Dump the stash contents!
foreach my $array_ref ( @stash ) {
 print "@$array_ref\n";
} 

An example of the screen output is as follows:

bash-3.2$ ./test_db_array.pl
2007 123 200728 200753 26
2008 256 200800 200852 53
2009 256 200900 200952 53
2010 258 201001 201052 52
2011 257 201101 201152 52
2012 256 201201 201253 53
2013 255 201300 201352 53
2014 254 201400 201452 53
2015 256 201500 201552 53
2016 116 201601 201624 24
bash-3.2$

Does @stash contain 10 rows by 5 columns, or does it contain 10 rows by 1 column (composing of a line of text)? How can I figure this out ?

In the development of my little program, I wish to loop through the year column (first column of the array), and print the year.

How can I access a specific element, for instance row 3 -- column 4?

simbabque
54.4k8 gold badges77 silver badges141 bronze badges
asked Jun 21, 2016 at 8:33
3
  • 1
    Use Data::Dumper to inspect data structures. Commented Jun 21, 2016 at 8:38
  • 1
    I dont' quite understand what you are asking You are accessing all elements -- looping through the top-level array, and printing out arrays whose refs are its elements. Instead of printing @$array_ref summarily, run a loop over it and you'll see exactly what is what. You've got everything right in the code you show, I'd suggest just play with it a little more. A good resource is perldsc, and of course perlreftut and perlref. Commented Jun 21, 2016 at 9:03
  • In addition to the above comment, you need to know that doing print "@array" will convert all elements of @array to a string. It's like a join(' ', @array) (where the ' ' is really the content of $", but that's not really important right now). So it's not a string, you just turn it into one through the way you output it. You also make a copy of the reference in your query sub because you don't have my in front of $array_ref = $sth->fetchrow_arrayref, which reuses the same variable, and then deref and ref it with [ @$array_ref ]. Do my $res and push @sub_stash, $res. Commented Jun 21, 2016 at 9:08

1 Answer 1

6

Your @stash array contains ten array references. Each of the referenced arrays will contain five elements (one from each of the columns in the query).

You can verify this by using Data::Dumper.

print Dumper(\@stash);

You can access individual elements of the sub-arrays using two sets of array-lookup brackets.

print $stash[0][0]; # prints 2007
print $stash[9][4]; # prints 24

Or you can print individual elements from the array within your loop.

foreach my $array_ref ( @stash ) {
 print "$array_ref->[0]\n"; # prints the first element (i.e. the year)
} 

For more details on dealing with these data structures, see perllol and perldsc.

answered Jun 21, 2016 at 9:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.