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

Hi,

I'm pretty new to perl and have hit a wall with a script i am trying to write. I want to add two potential values for each key in a hash. I am attempting to do it as below but i'm not sure if it is correct

$uniq{"$file"} = ({"suffix1"=>"$suff"}); $uniq{"$file"} = ({"suffix2"=>"$suff"});
I am also then trying to read each value by executing something like
foreach my $key (sort keys %uniq){ if ((exists($uniq{$key}{suffix1}) && ($uniq{$key}{suffix2}))){ .....
Is this correct..or even possible??

Cheers!

Replies are listed 'Best First'.
Re: Multiple values per key in a hash
by moritz (Cardinal) on May 13, 2008 at 10:06 UTC
    You can stick references into your hash, and these references can point to arrays, hashes or whatever you want. Read more about that in perlreftut.
Re: Multiple values per key in a hash
by TOD (Friar) on May 13, 2008 at 10:09 UTC
    $uniq{$file} = { 'suffix1' => $foo, 'suffix2' => $bar };
    in principle you cannot assign multiple values to one key in a hashtable, because its a key -> value construct. but if the value is a reference, i.e. to a hashref, you can bypass this restriction. note that all values must be scalars or references, not arrays or hashtables.
    --------------------------------
    masses are the opiate for religion.
Re: Multiple values per key in a hash
by sasdrtx (Friar) on May 13, 2008 at 11:11 UTC

    The previous comments answer your question technically, and point you at the documentation that explains how to do what you're trying to do. However, I'd just like to point out that there are two features of Perl that are the foundation of complex data structures; you need to learn and understand these well:

    1. References - these are much like C++ references. They are essentially immutable pointers.
    2. Anonymous arrays and hashes - In particular, watch that you don't confuse a list to initialize a named array or hash with an anonymous array or hash. The difference is in the enclosing punctuation. Make sure you get that clear, and try to use a font that distinguishes parens from braces clearly.

    It also helps to draw a picture at the start, and use Data::Dumper after that.

    Edit: fixed grammar in 1st para.


    sas
Re: Multiple values per key in a hash
by mhearse (Chaplain) on May 13, 2008 at 10:53 UTC
    Another option would be to store the suffixes as an anonymous array.

    To define:

    $uniq{$file} = [ $suffix1, $suffix2 ];
    To access:
    print $uniq{$file}[0], "\n"; print $uniq{$file}[1], "\n";
Re: Multiple values per key in a hash
by linuxer (Curate) on May 13, 2008 at 10:34 UTC
    $uniq{"$file"} = ({"suffix1"=>"$suff"}); $uniq{"$file"} = ({"suffix2"=>"$suff"});

    You overwrite the first hashref (for suffix1) when you assign the hashref for suffix2.

    # assign hashref with first key-value-pair $uniq{"$file"} = {"suffix1" => "$suff"}; # add additional key-value-pair $uniq{"$file"}->{"suffix2"} = "$suff";

    perldoc perldsc should show you how to create complex data structures.

    edit1: syntax error fixed

    edit2: "perldoc perldsc" as link text

    edit3: "edit" history edited

Re: Multiple values per key in a hash
by Pancho (Pilgrim) on May 13, 2008 at 12:55 UTC