问题
*Editing to include the entire code
#Shell script
less file.txt | perl parseperl.pl > index.html
#This is the Perl file parseperl.pl
my @array;
while(<>)
{
($date,$name,$city) = split(",",$_,3);
%hash =(NY=>"NYC", Washington=>"Virginia", London=>"UK");
push @array, "$name, $city,$hash{$city}";
}
foreach(@array)
{
($date,$name,$city,$hash{$city}) = split(",",$_);
print "<tr>\n";
print "<td>$name</td>\n";
print "<td>$city</td>\n";
print "<td>$hash{$city}</td>\n";
}
The code for some reason takes the previous key's value in the output
For eg: this is the output
Joe NYC NY
Mary Washington NY
My input file is file.txt.
12/12 12:32:56,Joe,NYC
12/12 12:12:16,Mary,Washington
Please advise
Edit
My code works when I use this
($name, $city, $state) = split (/,/,$_)
来源:https://stackoverflow.com/questions/53732423/hash-key-value-not-being-retrieved-correctly