Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Traversing a hash with array refs as keys?

Reply
Thread Tools

Traversing a hash with array refs as keys?

 
 
Bryan
Guest
Posts: n/a
 
      04-06-2007
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

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-06-2007
Bryan wrote:
> 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/Ti...uctKeyed-0.03/




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-06-2007
Bryan wrote:
> 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


 
Reply With Quote
 
darkon
Guest
Posts: n/a
 
      04-06-2007
Jürgen Exner <(E-Mail Removed)> wrote:

> Bryan wrote:
>> 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.)
 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      04-07-2007
On 04/06/2007 12:32 PM, Bryan wrote:
> 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/

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-07-2007
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)

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      04-11-2007
Mumia W. <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> On 04/06/2007 12:32 PM, Bryan wrote:
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
How to pass around an Array of Arrays (or Array Refs) jerrykrinock@gmail.com Perl Misc 3 05-13-2008 04:24 PM
Are weak refs slower than strong refs? John Nagle Python 3 02-25-2007 08:54 PM
Multiple Inheritance: mixed base class refs (hash, array) Alfred Z. Newmane Perl Misc 6 05-02-2005 07:04 PM
Printing an array of hash refs Tony N. Perl Misc 11 09-24-2004 03:59 PM



Advertisments