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

dinesh has asked for the wisdom of the Perl Monks concerning the following question:

hi i want to know wheather we can build an array which will acta s an hash at runtime it is possible otherwise in versions 5.005 and above using the syntax
{key1=>arrayindexvalue1, key2=>arrayindexvalue2,..... }
for example
$arrayreference = [{first =>1,second =>2 } , "hello ","there"];
now this will work both as an array and a hash
print " $arrayreference->{first} \t $arrayreference->{second} \n"; >hello there print " $arrayreference->[1] \t $arrayreference->[2] \n"; >hello there
can i build this thype of array at runtime that is can i add elements to this array one by one rather than initializing them all at one time

thanks

dinesh

edited: Mon Jul 1 13:29:48 2002 by jeffa - added code tags

Replies are listed 'Best First'.
Re: making arrays that act like hashes at runtime
by davorg (Chancellor) on Jul 01, 2002 at 11:55 UTC

    Pseudohashes are an exprimental feature that was added in 5.005 and are now widely considered to be a mistake. They will be deprecated in 5.8 and removed completely in 5.10. I therefore recommend that you don't use then for any new development.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: making arrays that act like hashes at runtime
by cjf (Parson) on Jul 01, 2002 at 12:08 UTC
Re: making arrays that act like hashes at runtime
by amphiplex (Monk) on Jul 01, 2002 at 12:07 UTC
    Just for completeness' sake (as the others here said, it is propably not a good idea to use this 'feature'):

    a quote from "Object Oriented Perl" by Damian Conway:

    --- <quote>

    <quote> You can add new entries to a pseudo-hash, but it's a two step procedure. First, you add a new key-to-index mapping:
    $pseudo_hash->[0]->{"z"} = @{$pseudo_hash};
    which maps the key "z" onto the first unused index in the pseudo-hash array. After that, you can access the new entry directly, to assign it a value:
    $pseudo_hash->{"z"} = "value z";
    </quote> ---- </quote>

    which would translate to something like this in your example:
    my $a = [ {first=>1, second=>2}, "hello", "there" ]; print "$a->{first}\t$a->{second}\n"; $a->[0]->{'third'} = @{$a}; $a->{'third'} = "foo"; print "$a->{first}\t$a->{second}\t$a->{third}\n";

    ---- kurt
Re: making arrays that act like hashes at runtime
by perrin (Chancellor) on Jul 01, 2002 at 13:45 UTC
    You may be able to get what you need by using the enum module. It would let you do this:

    use enum qw(first=1 second); my @foo; @foo[first, second] = qw(hello world);
    That puts the constants in the package namespace though, which you might not want. It's good for making objects based on array refs. If this isn't what you're after, try ArrayHashMonster.
Re: making arrays that act like hashes at runtime
by bronto (Priest) on Jul 01, 2002 at 13:51 UTC

    You could use fields and fields::phash in particular. Documentation reads:

    fields::phash() can be used to create and initialize a plain (unblessed) pseudo-hash. This function should always be used instead of creating pseudo-hashes directly.

    Nevertheless, I subscribe other's advice about not using pseudohashes at all. I don't know if even the fields implementation of pseudohashes will disappear or will be the "official way" of using pseudohash-like structures.

    Is there any monk that could give us an advice on this?

    Thanks

    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Re: making arrays that act like hashes at runtime
by Sifmole (Chaplain) on Jul 01, 2002 at 11:28 UTC
    This sounds alot like something called "psuedohashes". I believe Damian Conway's book "Object Oriented Perl" has a very good example of their use and information about them.

    Where are we linking to for books, now that FatBrain has been swallowed by B&N?