2013-03-08 13 views
8

Qualcuno sa come creare un hash con coppie di stringhe che servono come chiavi in ​​perl?Coppie come chiavi hash

Qualcosa di simile ...

{ 
    ($key1, $key2) => $value1; 
    ($key1, $key3) => $value2; 
    ($key2, $key3) => $value3; 

ecc ....

+0

correlati: [Hash :: multitasto] (http://p3rl.org/Hash::MultiKey) [Tie :: RefHash] (http://p3rl.org/Tie::RefHash) [Perl può memorizzare un riferimento di matrice come chiave hash?] (Http: // stackoverflow.it/questions/3083433/can-perl-store-an-array-reference-as-a-hash-key) [Come utilizzare un 'riferimento di subroutine' come chiave hash] (http://stackoverflow.com/questions/10795386/how-to-use-a-subroutine-reference-as-a-hash-key) – daxim

risposta

12

Non si può avere una coppia di scalari come una chiave hash, ma si può fare un multilivello hash:

my %hash; 
$hash{$key1}{$key2} = $value1; 
$hash{$key1}{$key3} = $value2; 
$hash{$key2}{$key3} = $value3; 

Se si desidera definire tutto in una volta:

my %hash = ($key1 => { $key2 => $value1, $key3 => $value2 }, 
      $key2 => { $key3 => $value3 }); 

in alternativa, se funziona per la vostra situazione, si può solo concatenare la chiave s insieme

$hash{$key1 . $key2} = $value1; # etc 

Oppure aggiungi un delimitatore per separare i tasti:

$hash{"$key1:$key2"} = $value1; # etc 
+4

il tuo primo suggerimento è ottimo, come per il secondo, è meglio avere un separatore per evitare collisioni come 123 456 e 12 3456 ... – Lucas

+3

Ci sono caratteri ASCII definiti proprio per questo scopo; il Separatore unità (es. 1f) è probabilmente il più adatto. – chepner

+0

Virgola, non no. – tchrist

1

faccio questo:

{ "$key1\x1F$key2" => $value, ... } 

Di solito con un metodo di supporto:

sub getKey() { 
    return join("\x1F", @_); 
} 

{ getKey($key1, $key2) => $value, ... } 

--- - EDIT -----

Aggiornato il codice qui sopra per utilizzare il separatore modulo ASCII per il recommendation from @chepner above

+0

Oppure è possibile impostare $; a "\ x1F" e usa $ hash {$ chiave1, $ chiave2} = $ valore o {"$ chiave1 $; $ chiave2" => $ valore} – MkV

+0

@MkV, devi amore perl ... mooooolto tanti modi per fare tutto :) – Lucas

1

Uso: multipla chiavi di un singolo valorein un hash possono essere utilizzati per l'attuazione di un 2D matrice o matrice N-dimensionale!

#!/usr/bin/perl -w 
use warnings; 
use strict; 
use Data::Dumper; 

my %hash =(); 

my ($a, $b, $c) = (2,3,4); 
$hash{"$a, $b ,$c"} = 1; 
$hash{"$b, $c ,$a"} = 1; 

foreach(keys(%hash)) 
{ 
    my @a = split(/,/, $_); 
    print Dumper(@a); 
} 
2

È possibile usare un invisible separator aderire le coordinate:

Principalmente per la matematica, l'invisibile Separator (U+2063) fornisce un separatore tra i caratteri dove punteggiatura o lo spazio possono essere omesse come in due -indice tridimensionale come i⁣j.

#!/usr/bin/env perl 

use utf8; 
use v5.12; 
use strict; 
use warnings; 
use warnings qw(FATAL utf8); 
use open qw(:std :utf8); 
use charnames qw(:full :short); 

use YAML; 

my %sparse_matrix = (
    mk_key(34,56) => -1, 
    mk_key(1200,11) => 1, 
); 

print Dump \%sparse_matrix; 

sub mk_key { join("\N{INVISIBLE SEPARATOR}", @_) } 

sub mk_vec { map [split "\N{INVISIBLE SEPARATOR}"], @_ } 
~/tmp> perl mm.pl |xxd 
0000000: 2d2d 2d0a 3132 3030 e281 a331 313a 2031 ---.1200...11: 1 
0000010: 0a33 34e2 81a3 3536 3a20 2d31 0a   .34...56: -1.
0

Usa $; implicitamente (o esplicitamente) nelle chiavi di hash, utilizzati per l'emulazione multidimensionale, in questo modo:

my %hash; 
$hash{$key1, $key2} = $value; # or %hash = ($key1.$;.$key2 => $value); 
print $hash{$key1, $key2} # returns $value 

È anche possibile impostare $; a \ x1f se necessario (il valore predefinito è \ 034, da SUBSEP in awk):

local $; = "\x1F"; 
Problemi correlati