[vox-tech] perl question - arrays and lists

Ken Herron vox-tech@lists.lugod.org
Thu, 08 May 2003 13:54:20 -0700


--On Thursday, May 08, 2003 13:12:43 -0700 Peter Jay Salzman 
<p@dirac.org> wrote:

> sub blah()
> {
>    foreach (@foobar)
>    {
>       push @array, ($foo, $bar);
>    }
>
>    return @array;
> }

Your "push" just pushes two simple values into @array. You're looking for 
this:

	push @array, [$foo, $bar];

Here, the [...] construct defines an anonymous array containing $foo and 
$bar. The (single) value pushed into @array is a reference to this 
anonymous array.

Later you can do things like this to access the individual elements:

	@barfoo = blah();
	foreach my $bf (@barfoo) {
		print $bf->[0], $bf->[1];
	}
or
	print $barfoo[0]->[0], $barfoo[0]->[1];
or
	print $barfoo[0][0], $barfoo[0][1];
or
	push @{$barfoo[0]}, qw(
		well hello dolly
		its so nice to have you back where you belong
	);

-- 
Kenneth Herron  Kherron@newsguy.com     916-366-7338