http://qs321.pair.com?node_id=307337


in reply to Hashing multiple items

I'm not totally sure I understand the question, but what if you just make the coordinates into a string and use them as a key for the hash?
my ($coord1, $coord2); $hash{$coord1 . "," . $coord2} = 'value';
Are you sure it was a book? Are you sure it wasn't.....nothing?

Replies are listed 'Best First'.
Re: Re: Hashing multiple items
by etcshadow (Priest) on Nov 15, 2003 at 17:37 UTC
    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
      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.