Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

jerryk...@gmail.com

unread,
May 13, 2008, 10:56:39 AM5/13/08
to
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

Joost Diepenmaat

unread,
May 13, 2008, 11:02:30 AM5/13/08
to
jerryk...@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/

A. Sinan Unur

unread,
May 13, 2008, 11:09:42 AM5/13/08
to
jerryk...@gmail.com wrote in news:14236a89-69f8-4b1a-bf24-
095e9c...@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)


--
A. Sinan Unur <1u...@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

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

Jürgen Exner

unread,
May 13, 2008, 12:24:30 PM5/13/08
to
jerryk...@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

0 new messages