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

Re: Re: Hashing multiple items

by etcshadow (Priest)
on Nov 15, 2003 at 17:37 UTC ( [id://307338]=note: print w/replies, xml ) Need Help??


in reply to Re: Hashing multiple items
in thread Hashing multiple items

Actually, perl has a built-in facility for this... if you simply say:
$hash{$coord1,$coord2} = $value;
then perl automatically constructs a hash key as:
$key = join($;, ($coord1, $coord2));
using $;, the "subscript separator" perl variable. The default value of which is octal 034, the control character "field separator". This isn't quite a perfect solution if your variables can store arbitrary binary data... but, in general, you're pretty safe against occurences of this character occuring in your data. You can perldoc perlvar for more details.

------------
:Wq
Not an editor command: Wq

Replies are listed 'Best First'.
Re: Re: Re: Hashing multiple items
by Anonymous Monk on Nov 15, 2003 at 17:43 UTC
    I actually want to do thngs the other way around. I want to have the zone as the key. So that I could say, print out out all the coorsinates in zone B1. Doing this -
    $coordinates{$zone} = $a1,$a2,$b1,$b2)
    will only allow my to save one coordinate for a particular zone. How do I save an arbitary bumber of coordinates for each zone?
      Use an anonymous array as value:
      $coordinates{$zone} = [ $a1, $a2, $b1, $b2 ];
      Don't forget to dereference it when you read your coordinates back out:
      ($a1, $a2, $b1, $b2) = @{$coordinates{$zone}}
      Have a look at perlref and perlreftut.
      Hope this helped.
      CombatSquirrel.
      Entropy is the tendency of everything going to hell.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (None)
    As of 2024-04-19 00:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found