Hash key value not being retrieved correctly

╄→尐↘猪︶ㄣ 提交于 2020-01-25 11:13:12

问题


*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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!