in reply to Re: Re: Passing Array of Arrays
in thread Passing Array of Arrays

Eh?

I was always told that "complex" structures (anything other than a "simple" array or hash - and the OP is talking about an AoA) got flattened, and that its integrity wasn't necessarily preserved when it was passed into and out of a sub.

I'm quite happy to be wrong, though .. that's been a pet peeve of mine with Perl for *ages* ... *grin* ..

My thinking this (probably) comes from perlsub ..

Perl sees all arguments as one big, long, flat parameter list in @_. Like the flattened incoming parameter list, the return list is also flattened on return.

I read this as saying that if I have a crazily bizarre structure, such as an AoA or AoH or whatever, its integrity and structure will not necessarily be preserved - epecially not if it becomes a simple list of scalars.

Can anyone clarify this? I'm gonna play with some code, see what happens ..

Update: Ok, so code playing proves I'm completely wrong ... all kudos to broquaint - I've learnt something completely new today .. and got rid of a pet hate :)

-- Foxcub
A friend is someone who can see straight through you, yet still enjoy the view. (Anon)

Replies are listed 'Best First'.
Re: Re: Re: Re: Passing Array of Arrays
by broquaint (Abbot) on Mar 11, 2003 at 13:57 UTC
    Right, in list context an arrays and hashes are flattened into a single list e.g
    my @a = qw( foo bar baz ); my %h = qw( one ichi two ni ); ## print evaluates it's args in list context print "array: ", @a,$/; print "hash: ", %h,$/; print "a & h: ", @a, %h, $/; __output__ array: foobarbaz hash: oneichitwoni a & h: foobarbazoneichitwoni
    Now with nested data structures this is still exactly the same as references aren't automagically dereferenced e.g
    my @a = ( ["one"], ["two"] ); my %h = ( a => \@a ); print "array: ", @a, $/; print "hash: ", %h, $/; __output__ array: ARRAY(0x80fbb0c)ARRAY(0x80fba1c) hash: aARRAY(0x8107d84)
    So because subroutines' arguments are evaluated in list context an AoA is fine and dandy to pass about without having to create a reference to it.
    HTH

    _________
    broquaint

(z) Re^4: Passing Array of Arrays
by zigdon (Deacon) on Mar 11, 2003 at 13:53 UTC

    When you build an AoA, you're just building an array, who's values are references to other arrays. So while you write:

    @a = ( [ qw/1 2 3/ ], [ qw/a b c/ ] );
    You're actually creating 3 arrays, and put in @a references to the other two.

    So when you pass that to a sub, it's passed as a simple list - but the members of the list are still the references to your two arrays, and perl doesn't really care if they're scalars or references - there's no automatic-recursive-dereferencing going on...

    Does that help at all?

    -- zigdon