Traversing a hash with array refs as keys?


B

Bryan

Another one...

I have a structure of key value pairs, where the key is an array, and
the value is a scalar. I would have preferred to use the scalars as the
keys, but there may be duplicates, whereas I know the arrays do not.

In passing in such a structure to a function by reference, how do I
assign a local var to the array ref inside the hash ref?

I need to traverse over the thing:

while ((my $array_ref, my $value) = each(%$hash_ref)) {...}
Doesnt work, it thinks $array_ref is a string with "REF(0x1e17594)" as a
value.

while((my @$array_ref...
Doesnt work, dereferencing error.

Im starting to think I am using the wrong structure, but that is really
the structure of my data: 1-1 key-value relationships, where the array
is the unique element and therefore the key. Right?

Dunno. How do I get my array back? I create it like so btw:
$hashref->{\[@key_array]} = $value;

Maybe that is messing me up.

Help?

B
 
Ad

Advertisements

J

John W. Krahn

Bryan said:
Another one...

I have a structure of key value pairs, where the key is an array, and
the value is a scalar. I would have preferred to use the scalars as the
keys, but there may be duplicates, whereas I know the arrays do not.

In passing in such a structure to a function by reference, how do I
assign a local var to the array ref inside the hash ref?

I need to traverse over the thing:

while ((my $array_ref, my $value) = each(%$hash_ref)) {...}
Doesnt work, it thinks $array_ref is a string with "REF(0x1e17594)" as a
value.

That is because hash keys *are* strings and whatever you use as a hash key
will be converted to a string. Once a reference is converted to a string it
can not be converted back to a reference.

while((my @$array_ref...
Doesnt work, dereferencing error.

Im starting to think I am using the wrong structure, but that is really
the structure of my data: 1-1 key-value relationships, where the array
is the unique element and therefore the key. Right?

Dunno. How do I get my array back? I create it like so btw:
$hashref->{\[@key_array]} = $value;

You are storing a reference to a reference to an array (an anonymous array is
already a reference.)

Perhaps either of these modules will do what you want:

http://search.cpan.org/~fxn/Hash-MultiKey-0.06/
http://search.cpan.org/~osfameron/Tie-Hash-StructKeyed-0.03/




John
 
J

Jürgen Exner

Bryan said:
I have a structure of key value pairs, where the key is an array,

That is impossible. Hash keys can only be strings. Any other scalar value is
converted into its string representation.
the value is a scalar. I would have preferred to use the scalars as
the keys, but there may be duplicates, whereas I know the arrays do
not.
In passing in such a structure to a function by reference, how do I
assign a local var to the array ref inside the hash ref?

So is your key an array or an array reference? Those two things are quite
different.
I need to traverse over the thing:

while ((my $array_ref, my $value) = each(%$hash_ref)) {...}
Doesnt work, it thinks $array_ref is a string with "REF(0x1e17594)"
as a value.

Correct. As mentioned above keys must be strings.
while((my @$array_ref...
Doesnt work, dereferencing error.

Not surprising.
Im starting to think I am using the wrong structure,

Quite possible.
but that is
really the structure of my data: 1-1 key-value relationships, where
the array is the unique element and therefore the key. Right?

This smells very much like an x-y problem. Do you actually use that hash as
a hash, i.e. is there really a logical function, that maps your arrays to
the value? Hashes are ideal for lookups. Do you really get some array from
somewhere and frequently need to look up the associated value for that array
in constant time?

If not then chances are a different data structure would be better suited
for the job.

jue
 
D

darkon

Jürgen Exner said:
Bryan said:
I have a structure of key value pairs, where the key is an
array,

That is impossible. Hash keys can only be strings. Any other
scalar value is converted into its string representation.
[snip]
Im starting to think I am using the wrong structure,

Quite possible.
but that is
really the structure of my data: 1-1 key-value relationships,
where the array is the unique element and therefore the key.
Right?

This smells very much like an x-y problem. Do you actually use
that hash as a hash, i.e. is there really a logical function,
that maps your arrays to the value? Hashes are ideal for
lookups. Do you really get some array from somewhere and
frequently need to look up the associated value for that array
in constant time?

If not then chances are a different data structure would be
better suited for the job.

He could switch to Python and use tuples as the keys. :)

(Considers.... Nah.)
 
M

Mumia W.

Another one...

I have a structure of key value pairs, where the key is an array, and
the value is a scalar. [...]

As the others have said, hash keys must always be scalar strings;
however, I think there's a module on CPAN that allows you to use
references as hash keys. It might be Tie::RefHash, but search CPAN
yourself: http://search.cpan.org/
 
D

Dr.Ruud

Bryan schreef:
I have a structure of key value pairs, where the key is an array, and
the value is a scalar.

So you have a segmented key. Is the number of segments the same for all?

You can use

$HoH{$keypart1}{$keypart2}{$keypart3} = $value;

(see perldsc)

or

$value{$keypart1, $keypart2, $keypart3} = $value;

(see $; in perlvar)
 
Ad

Advertisements

A

anno4000

Mumia W. said:
Another one...

I have a structure of key value pairs, where the key is an array, and
the value is a scalar. [...]

As the others have said, hash keys must always be scalar strings;
however, I think there's a module on CPAN that allows you to use
references as hash keys. It might be Tie::RefHash, but search CPAN
yourself: http://search.cpan.org/

Tie::RefHash is a standard module.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top