散列键值没有被正确检索(Hash key value not being retrieved cor

2019-11-05 08:37发布

*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";
}

由于某种原因,该代码需要在输出之前键的值

对于如:这是输出

Joe NYC NY
Mary Washington NY

我输入文件file.txt的。

12/12 12:32:56,Joe,NYC
12/12 12:12:16,Mary,Washington

请指教


编辑

当我使用这个我的代码工作

($name, $city, $state) = split (/,/,$_)
文章来源: Hash key value not being retrieved correctly
标签: perl hash