Ho un array e ho provato a convertire il contenuto dell'array in un hash con chiavi e valori. L'indice 0 è una chiave, indice 1 è un valore, indice 2 è una chiave, indice 3 è un valore, eccCome posso convertire un array in un hash in Perl?
Ma non sta producendo il risultato atteso. Il codice è qui sotto:
open (FILE, "message.xml") || die "Cannot open\n";
$var = <FILE>;
while ($var ne "")
{
chomp ($var);
@temp = split (/[\s\t]\s*/,$var);
push(@array,@temp);
$var = <FILE>;
}
$i = 0;
$num = @array;
while ($i < $num)
{
if (($array[$i] =~ /^\w+/i) || ($array[$i] =~ /\d+/))
{
# print "Matched\n";
# print "\t$array[$i]\n";
push (@new, $array[$i]);
}
$i ++;
}
print "@new\n";
use Tie::IxHash;
tie %hash, "Tie::IxHash";
%hash = map {split ' ', $_, 2} @new;
while ((my $k, my $v) = each %hash)
{
print "\t $k => $v\n";
}
l'output prodotto non è corretto:
name Protocol_discriminator attribute Mandatory type nibble value 7 min 0 max F name Security_header attribute Mandatory type nibble value 778 min 0X00 max 9940486857
name => Security_header
attribute => Mandatory
type => nibble
value => 778
min => 0X00
max => 9940486857
In uscita si può vedere che l'hash è formato solo da una parte, e un'altra parte della matrice non è essere creato nell'hash.
Qualcuno può aiutare?
Supponendo che i dati in @array viene memorizzato come chiave, valore, chiave, valore, ecc – Hawk
@Hawk e i suoi elettori: per favore prenditi il tuo tempo per leggere la domanda. OP lo afferma già nella sua domanda. – m0skit0
Grazie @Hawk, una nota molto utile per quelli di noi che leggono solo il titolo e subito saltare alle risposte;) – rustyx