Click here to get back home

How to pass around an Array of Arrays (or Array Refs)

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
How to pass around an Array of Arrays (or Array Refs) jerrykrinock 05-13-2008
Posted by jerrykrinock on May 13, 2008, 10:56 am
Please log in for more thread options
I need to pass an array of arrays from a function, but I can't make an
array of arrays work. Actually, I've got better results making an
array of array references. Here's what I've done:

#!/usr/bin/perl

my @array ;
my @row ;
@row = ('a00', 'a01') ;
push (@array, \@row) ;
@row = ('a10', 'a11') ;
push (@array, \@row) ;
@row = ('a20', 'a21') ;
push (@array, \@row) ;

# In real life I will return @array to the caller, but the
# problem can be demonstrated without doing that...

my $nRows = @array ;
foreach my $array_ref (@array) {
# $array_ref is a reference to an array (row)
# Dereference it.
my @row = @$array_ref ;
# Print each element (column) separated by spaces
foreach my $element (@row) {
print "$element ";
}
# Next row, new line
print "\n" ;
}

My expected result is:

a00 a01
a10 a11
a20 a21

but the actual result is:

a20 a21
a20 a21
a20 a21

What am I doing wrong?

Thanks,

Jerry Krinock

Posted by Joost Diepenmaat on May 13, 2008, 11:02 am
Please log in for more thread options
jerrykrinock@gmail.com writes:

> I need to pass an array of arrays from a function, but I can't make an
> array of arrays work. Actually, I've got better results making an
> array of array references. Here's what I've done:
>
> #!/usr/bin/perl
>
> my @array ;
> my @row ;
> @row = ('a00', 'a01') ;
> push (@array, \@row) ;
> @row = ('a10', 'a11') ;
> push (@array, \@row) ;
> @row = ('a20', 'a21') ;
> push (@array, \@row) ;

<snip>

> What am I doing wrong?

You're reusing the same array for each row; each row is a reference to
the same array (@row), which means you end up with each row containing
the values you last assigned to @row.

You want something like:

my @array ;
my @row ;
@row = ('a00', 'a01') ;
push (@array, [ @row ]); # [ @row ] creates a reference to a new array
# with copies of the values in @row.
@row = ('a10', 'a11') ;
push (@array, [ @row ]);
@row = ('a20', 'a21') ;
push (@array, [ @row ]);


or less ugly:

my @array = ( [ 'a00', 'a01' ],
[ 'a10', 'a11' ] );

push @array, ['a20','a21'];


--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by A. Sinan Unur on May 13, 2008, 11:09 am
Please log in for more thread options
jerrykrinock@gmail.com wrote in news:14236a89-69f8-4b1a-bf24-
095e9cd0db9d@p25g2000hsf.googlegroups.com:

> I need to pass an array of arrays from a function, but I can't make an
> array of arrays work. Actually, I've got better results making an
> array of array references. Here's what I've done:
>
> #!/usr/bin/perl
>
> my @array ;
> my @row ;
> @row = ('a00', 'a01') ;
> push (@array, \@row) ;
> @row = ('a10', 'a11') ;
> push (@array, \@row) ;
> @row = ('a20', 'a21') ;
> push (@array, \@row) ;

Would it be easier to understand if I changed the above to:

my $array_ref = [
[ qw( a00 a01 ) ],
[ qw( a10 a11 ) ],
[ qw( a20 a21 ) ],
];

In what you wrote above, you are pushing the reference to the same array
(@row) and you keep overwriting the contents of @row. So, now you have
three references pointing to the exact same array.

#!/usr/bin/perl

use strict;
use warnings;

my @array;

my @row = ('a00', 'a01') ;
push (@array, \@row) ;
@row = ('a10', 'a11') ;
push (@array, \@row) ;
@row = ('a20', 'a21') ;
push (@array, \@row) ;

print "$_\n" for @array;

__END__

E:\Home\asu1\Src\Test> s1
ARRAY(0x182a2fc)
ARRAY(0x182a2fc)
ARRAY(0x182a2fc)


--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by Jürgen Exner on May 13, 2008, 12:24 pm
Please log in for more thread options
jerrykrinock@gmail.com wrote:
>I need to pass an array of arrays from a function, but I can't make an
>array of arrays work.

That's because you can't. The argument list as well as the return value
of a function is a flat sequence of scalars. It is impossible to pass an
array of arrays (or list of arrays for that matter).

>Actually, I've got better results making an
>array of array references. Here's what I've done:

And that's exactly what you should be doing.

jue

Similar ThreadsPosted
How to pass 2D array to sub function and return 2D array? August 3, 2006, 4:31 am
Printing an array of hash refs September 22, 2004, 9:35 am
Traversing a hash with array refs as keys? April 6, 2007, 1:32 pm
Multiple Inheritance: mixed base class refs (hash, array) April 29, 2005, 2:29 am
pass 2d array to C function August 28, 2004, 9:27 am
How to pass an array and scalar as arguments to a subroutine/ June 28, 2006, 12:16 pm
Stuck trying to pass an array that contains a hash to another subprogram March 21, 2007, 6:54 pm
Please help me pass an array from VBA to Perl and populate it. Newbie at wits' end! October 2, 2006, 9:49 pm
FAQ 7.14: How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}? November 3, 2004, 6:03 am
FAQ 7.14: How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}? November 25, 2004, 6:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap