Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: non-scalar hash key

by Anonymous Monk
on Jun 17, 2009 at 09:57 UTC ( #772313=note: print w/replies, xml ) Need Help??


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

I think he want array of integers to become key, not value

Replies are listed 'Best First'.
Re^3: non-scalar hash key
by kdejonghe (Novice) on Jun 17, 2009 at 10:00 UTC
    exactly
      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 :)

        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

        A reference to another array with the same sequence of integers should point to the same hash entry.
      I'm wondering why this is so difficult. If you take a string as the key to a hash, that string is actually an array of bytes, which is probably searched for by the C function strcmp. Now all I want is an array of which the elements are a tad larger (4 bytes iso 1 byte, because I need unsigned longs), and possibly another hash function (hashword in http://burtleburtle.net/bob/c/lookup3.c) and another comparison function similar to strcmp but working on integers.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://772313]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2018-07-20 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (427 votes). Check out past polls.

    Notices?