嗨,我是新来的perl和初学者阶段请帮助我有哈希
%hash = { a => 2 , b=>6, a=>4, f=>2, b=>1, a=>1}
Run Code Online (Sandbox Code Playgroud)
我希望输出为
应该是一个新的哈希
%newhash = { a => 7, b=>7,f =>2}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
要计算我正在做的哈希键的频率
foreach $element(sort keys %hash) {
my $count = grep /$element/, sort keys %hash;
print "$element comes in $count times \n";
}
Run Code Online (Sandbox Code Playgroud)
但通过这样做我得到的输出为:
a comes 1 times
b comes 1 times
a comes 1 times
f comes 1 times
b comes 1 times
a comes 1 times
Run Code Online (Sandbox Code Playgroud)
这不是我想要的.
如何获得重复键的正确频率数?如何添加这些重复键的值并存储在新哈希中?
嗨,我是Perl的新手,在学习过程中.我有一个阵列
@array = ( 10, 40, 59, 40, 90, 100, 30, 40, 100, 20, );
Run Code Online (Sandbox Code Playgroud)
我想找到数组中的最大数字,并且还想知道数组中存在最大数字的索引.
我在做
my $maxValue = max @array;
print $maxValue; # displays the maximum number in the entire array
my ($index) = grep $array[$_] eq $maxValue , 0.. $#array;
print ($index); # this gives me the index of the maximum number which was found in the array.
Run Code Online (Sandbox Code Playgroud)
我得到的输出是100,索引为5
但实际上100在数组中会出现2次:一次在索引6处,再次在索引8.我的代码只提供了它找到的第一个索引,其中包含最大值.
如何获得具有最大值的所有索引?
嗨,我是Perl编程的新手.我正在尝试读取一个.csv由逗号分隔的2个字段的文件.
我想以密钥及其值的形式将文件的所有数据放入哈希.
我的输入文件是
2.8, gitu 2.5, Has 2.7 Hwait 3.1-weiity 4.2, city 2.7:query 4.9, city 16.2, play 6.2, game 7,,,8 Jami 4.0, city
此行代码不会打印输入文件中存在的所有有效数据.有效数据采用行开头的形式,数字之间有逗号,然后是名称.否则应忽略其他无效条目.当我打印时,它会跳过几个有效的条目显示%hashforHighMagnitude;
请告诉我我在哪里丢失?如何获得所有有效条目%hashforHighMagnitude;
我想这样做
open ( OF, "$inputFile") or die "Cant open input file: $!\n";
while ( $Line =<OF>) {
if($Line =~ /^\d+\.+\d*\s*,\s*\w+$/g)
{
( my $magnitude, my $place ) = split(/,/,$Line);
$hashforHighMagnitude{$place} = $magnitude;
$hash{$place}++;
}
else
{
next;
}
}
print %hashforHighMagnitude;
close(OF);
Run Code Online (Sandbox Code Playgroud)
输出应该是
2.8, gitu 2.5, Has 4.2, city 4.9, …