*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 (/,/,$_)