Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: non-scalar hash key

by McDarren (Abbot)
on Jun 17, 2009 at 10:09 UTC ( #772317=note: print w/ replies, xml ) Need Help??


in reply to Re^3: non-scalar hash key
in thread non-scalar hash key

ah, okay.

Well, a reference to an array is a scalar. So, perhaps something like:

my @foo = qw/1 2 3 4 5 6/; my %bar = ( \@foo => 'elephants', ); my $zonk = \@foo; print "$bar{$zonk}\n";
prints:
elephants

Cheers,
Darren :)


Comment on Re^4: non-scalar hash key
Select or Download Code
Re^5: non-scalar hash key
by kdejonghe (Novice) on Jun 17, 2009 at 10:13 UTC
    A reference to another array with the same sequence of integers should point to the same hash entry.
Re^5: non-scalar hash key
by citromatik (Curate) on Jun 17, 2009 at 10:33 UTC

    The problem with your suggestion is that two arrays with the same elements will give two entries in the hash (because they will have different references). Consider:

    use strict; use warnings; use Data::Dumper; my @foo = qw/1 2 3 4 5 6/; my @fu = qw/1 2 3 4 5 6/; my %bar = ( \@foo => 'elephants', \@fu => 'zebras' ); print Dumper \%bar;

    Outputs something like:

    $VAR1 = { 'ARRAY(0x18ae770)' => 'zebras', 'ARRAY(0x17f0ec8)' => 'elephants' };

    And:

    my @foo = qw/1 2 3 4 5 6/; my @fu = qw/1 2 3 4 5 6/; my %bar = ( join ('',@foo) => 'elephants', join ('',@fu) => 'zebras' ); print Dumper \%bar;

    Outputs:

    $VAR1 = { '123456' => 'zebras' };

    citromatik

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://772317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-09-17 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (72 votes), past polls