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

I need to populate an array of key value pairs with the possiblity of having duplicate keys like so:
my $array_of_key_value_pairs = [ item1 => $foo$, item2 => $bar$, item1 => $foo2, ]
I though the syntax would be something like this:
my $data; ... push @$data, "$key => $value;
this doesn't seem to work...any thoughts

Replies are listed 'Best First'.
Re: array of key value pairs?
by chromatic (Archbishop) on Mar 18, 2008 at 19:27 UTC

    Did you mean:

    my $data = []; push @$data, $key => $value;
      that is exactly what I meant...it's working. Thanks
Re: array of key value pairs?
by FunkyMonk (Bishop) on Mar 18, 2008 at 21:38 UTC
    You do know that an "array of key value pairs" is just a flat list, don't you?

    Consider...

    my ( $foo, $bar, $foo2 ) = qw/foo bar foo2/; my $array_of_key_value_pairs = [ item1 => $foo, item2 => $bar, item1 => $foo2, ]; print Dumper $array_of_key_value_pairs;

    Output:

    $VAR1 = [ 'item1', 'foo', 'item2', 'bar', 'item1', 'foo2' ];

Re: array of key value pairs?
by dragonchild (Archbishop) on Mar 19, 2008 at 01:57 UTC
    You probably want to look at Tie::Hash::MultiValue instead.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?