http://qs321.pair.com?node_id=20093


in reply to RE: My views on pseudohashes
in thread My views on pseudohashes

am I missing something .. aren't pseudo-hashes and anonymous arrays the same thing ?

Replies are listed 'Best First'.
RE: RE: My views on pseudohashes
by extremely (Priest) on Jun 29, 2000 at 18:57 UTC
    No they aren't. An anonymous array is an array without a formal name. Like:
    my $q = [ $a, $b, $c, $d];
    returns an anonymous array ref into $q, you created an array without ever declaring an named array like with `my'.

    A pseudo-hash is an array (anonymous or not) that has a special first entry. Pseudo-hashes are still listed in `perldoc perlref' as experimental. try this:

    my @ph; $ph[0]={Foo=>1, Bar=>2, Bag=>3}; $ph[1]="what Foo points to"; $ph[2]="what Bar points to"; $ph[3]="what Bag points to"; print "$ph{Foo} is $ph[1]\n";

    In effect making the first item in an array a hash where each key points to a row index lets you use the array like a hash. All you favorite hash tricks work, `keys', `values', `exists' and more. The real win here is that you have an intrisic order to the hash thanks to the array, but the hash tools won't naturally return in that order, you have to do it yourself. Also, you can't add a new field with a simple `$ph{New}="a new item"' cause it doesn't know how to do it.

    If you decide to play with em, PLEASE read the `perldoc perlref' section on it. There are some handy-dandy tools in perl 5.6 like `perldoc fields' that will add some compile time sanity and ease of use to them.

    HTH somebody. =)

    -- mark