Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Arrays as Hash Values

by c (Hermit)
on Aug 16, 2001 at 02:27 UTC ( [id://105213]=perlquestion: print w/replies, xml ) Need Help??

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

I read this which goes over using a list as a value for an array. However, the example is in the format of what in my mind, I call "pushing" the values into the hash. I currently have a script that is written with hashes in the format of:

my %hash = ( key1 => "value1", key2 => "value2", key3 => "value3" );

So, I would like to just %s/old/new/gc and update my current hashes. When i add in:

["value1a","value1b","value1c"]

I get the huge hint of ARRAY(0x80cb874))[1] when I go to print a value. I've seen this in my books, unfortunately, I just sold my intro book and I'm waiting for its replacement.

humbly -c

Replies are listed 'Best First'.
Re: Arrays as Hash Values
by japhy (Canon) on Aug 16, 2001 at 02:41 UTC
    You want to do
    $hash{key} = [ 'this', 'that', 'those' ];
    To print one of those values in a string, you'd do:
    print "The 2nd value is $hash{key}[1].\n";

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Arrays as Hash Values
by dragonchild (Archbishop) on Aug 16, 2001 at 02:45 UTC
    I'm going to take a stab and say that you misunderstood the Categorized Questions and Answers node. It's not that you use the arrayref itself as the key, but that the value is an arrayref. So, you would have something like:
    my %hash = ( key1 => [1, 2, 3], key2 => [5, 6, 7], );
    Then, when you wanted to get the values back, you'd do something like $hash{key1}[2] and that would give you 3.

    The problem with using an arrayref as a hashkey is that the reference gets stringified. That process ends up being divorced from the actual reference, meaning you can't get to it from the key itself.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

      Yes, I hear/see what you are saying, however, what if my array elements need to be interpolated:

      my %hash = ( key1 => ["one thing","one $thing","$another"], key2 => ["and the list","keeps $going"] );

      When I go down this road, I am seeing errors. Should I be? or is this syntax correct? Apologies for not getting into this prior.

      humbly -c

        As far as I can tell, that syntax looks to be just fine. What I would do at this point is to post an actual running script (doesn't have to be long) and its output, including errors. That way, we can help you more.

        ------
        /me wants to be the brightest bulb in the chandelier!

        Vote paco for President!

        try enclosing key1 and key2 in quotes
Re: Arrays as Hash Values
by Hofmator (Curate) on Aug 16, 2001 at 12:58 UTC

    And here - to make things complete - goes an example how to push values onto such an array stored in a hash:

    use Data::Dumper; my %hash; for my $key ('A'..'C') { for my $value (1..3) { push @{$hash{$key}}, $value; } } print Dumper(\%hash);

    -- Hofmator

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://105213]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-29 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found