在perl代码中,我试图通过键和&来初始化哈希。值存储在2个单独的数组中。 为了初始化数组,从文本文件&读取数据。然后处理。
我按照以下语法存储密钥&值为哈希值:
@hash {@key} = @值;
当我尝试显示哈希的内容时,我能够单独显示密钥的内容。不是价值观。为什么哈希没有采用任何值?如何解决这个问题?
文字档案
NAME,OWE,RECEIVE
RAM,2000,1000
TEJA,1500,2200
NANDHINI,400,3000
RAGHAV,0,5000
ETHI,100,2500
KESHAV,400,400
以下是我的代码:
$i = 0;
open(FH, "<expenses_details.txt") or die "Couldn't open the file";
%nameo;
while ($line = <FH>)
{
chomp($line);
if ($i == 0)
{
$i++;
next;
}
($name, $owe, $receive) = split(',', $line);
#print "Name is:$name, Owe:$owe, Receive:$receive \n";
push(@names, $name); # Creating name array
push(@owes, $owe); #creating owe array
push(@receives, $receive); #creating receive array
}
close FH;
print "Name array:\n";
foreach (@names)
{
print "$_\n";
}
print "\nOWE array:\n";
foreach (@owes)
{
print "$_\n";
}
#Initialising owe hash
@nameo{@names} = @owes;
$size = keys %nameo;
print "\nsize is $size\n";
foreach my $key (keys %nameo)
{
print $key;
print $nameo[$key];
print "\n";
}
获得的输出:
Name array:
RAM
TEJA
NANDHINI
RAGHAV
ETHI
KESHAV
OWE array:
2000
1500
400
0
100
400
size is 6
TEJA
RAM
KESHAV
ETHI
NANDHINI
RAGHAV