in reply to Returning multiple arrays

Hope this helps

Regards
Manoj
#!/usr/bin/perl use warnings; use strict; my ($a, $array_a , $array_b) = p_sub_a( ) ; print @$array_a[0]; print @$array_a[1]; print @$array_b[0]; print @$array_b[1]; exit 0; sub p_sub_a { my $a; my @array_aa ; my @array_bb; $array_aa[0] = "1\n"; $array_aa[1] = "2\n"; $array_bb[0] = "1b\n"; $array_bb[1] = "2b\n"; return ($a, \@array_aa , \@array_bb); }

Code tags added by davido.

Replies are listed 'Best First'.
Re^2: Returning multiple arrays
by davido (Cardinal) on Feb 17, 2005 at 16:55 UTC

    There's a subtile mistake in your code. You're improperly dereferencing $array_a and $array_b. Yes, it's working for you ok, but consider the following code:

    perl -we "@array = qw/one two three/; print @array[1], $/;"

    And the warning is...

    Scalar value @array[1] better written as $array[1] at -e line 1.

    Why? Because @array[1] is an array slice of exactly one element. It's similar to @array[0 .. 3], except for being just one element. Perl prefers (for various good reasons) that you use slice semantics when you intend a slice, and scalar semantics when you mean a single element of an array.

    Now, why doesn't your example throw a warning? Because you're dealing with references, not simple arrays. I suppose nobody's thought to add such a warning to Perl's repertoire yet, but it probably ought to be there.

    The proper way to dereference $array_a would be like this:

    print $array_a->[0];

    Hope this clears up a potential future mistake... ;)


    Dave