Voglio creare dinamicamente un hash %detail
, senza utilizzare una dichiarazione eval
. Questo codice funziona correttamente con un'istruzione eval
, ma esiste un modo migliore per eseguire questa operazione senza utilizzare eval
?
my @input=('INFO: Vikram 32 2012','SAL: 12000$','ADDRESS: 54, junk, JUNK');
my %matching_hash= (
qr/^INFO:\s*(\S+)\s+(\S+)\s+(\S+)/ =>['name','age','joining'],
qr/^SAL:\s*(\S+)/ => ['salary'],
qr/ADDRESS:\s*(.*)/ =>['address']
);
my %detail;
while(my ($regex, $array) = each(%matching_hash)) {
foreach (@input){
if(/$regex/) {
for(my $i=0;$i<=$#$array; $i++) {
$j=$i+1;
eval '$detail{$array->[$i]} = $$j';
}
}
}
}
use Data::Dumper;
print Dumper(\%detail);
++++++++++++++
$VAR1 = {
'name' => 'Vikram',
'address' => '54, junk, JUNK',
'age' => '32',
'joining' => '2012',
'salary' => '12000$'
};
Non penso che la tua dichiarazione di valutazione funzionerà se si usa strict' (ho dovuto rimuoverlo dal mio test per ottenere l'output desiderato). –