Perl 散列键值打印问题
我没有计算Perl 散列键值打印问题,perl,hash,Perl,Hash,我没有计算$mapA{$Brand_Name}->{Success}$mapA{$Brand_Name}->{Failure}请对此提供帮助实际上我在另一个问题中提出了这个问题,即已结束,所以我在这里提出。 或者以任何其他方式增加特定键的计数 #!/usr/bin/perl use Text::CSV; use POSIX qw(strftime); use Data::Dumper; use LWP::Simple; my $APK_GCM="/root/Basavaraj/GCM/cdr
$mapA{$Brand_Name}->{Success}
$mapA{$Brand_Name}->{Failure}
请对此提供帮助实际上我在另一个问题中提出了这个问题,即已结束,所以我在这里提出。
或者以任何其他方式增加特定键的计数
#!/usr/bin/perl
use Text::CSV;
use POSIX qw(strftime);
use Data::Dumper;
use LWP::Simple;
my $APK_GCM="/root/Basavaraj/GCM/cdr_02-01-2018_StreamzGcm.csv";
my $WEB_GCM="/root/Basavaraj/GCM/cdr_02-01-2018_StreamzWebPushNotification.csv";
my $Yesterday= strftime ("%d-%m-%Y", localtime(time-86400));
my $Current_Date= strftime ("%d-%m-%Y",localtime(time));
print "$Yesterday \n";
print "$Current_Date \n";
open(STDOUT, '>', "/root/Basavaraj/STREAMZ_GCM_APK.txt");
#Creating Class to split the document line by line by comma ,
my $csv = Text::CSV->new({ sep_char => ',' });
open (my $data, '<:encoding(utf8)', $APK_GCM) or die "Could Not open File '$APK_GCM' $!\n";
open (my $data1,'<:encoding(utf8)', $WEB_GCM) or die "Could Not open File '$WEB_GCM' $!\n";
my %mapA;
my $dummyA =<$data>;
while (my $line = <$data>) {
if ($csv->parse($line)) {
my @fields = $csv->fields();
my $Brand_Name=$fields[2];
my $Streamz_Sent=$fields[5];
my $GoogleResA=$fields[5];
$mapA{$Brand_Name} = {Success =>0,Failure=> 0} unless exists ($mapA{$Brand_Name});
my $failureA='{error:MismatchSenderId}';
if ($GoogleResA eq $failureA){
$mapA{$Brand_Name}->{Failure}++;
print "$Brand_Name:$mapA{$Brand_Name}->{Failure} \n";
}else{
$mapA{$Brand_Name}->{Success}++;
print "$Brand_Name:$mapA{$Brand_Name}->{Success} \n";
}
} else {
warn "Line could not be parsed: $line\n";
}
}
#$, = ",";
print " $mapA{$Brand_Name}->{Failure} \n";
my $KeyA;
while (($keyA)=each (%mapA)){
my $success= $mapA{$Brand_Name}->{Success};
my $failure= $mapA{$Brand_Name}->{Failure};
print "$keyA $mapA{$Brand_Name}->{Failure}++ $mapA{$Brand_Name}->{Success}++ \n";
}
foreach my $name ( keys %mapA) {
print " $mapA{$Brand_Name}->{Failure} \n";
print " $mapA{$Brand_Name}->{Success} \n";
print "$name $mapA{$Brand_Name}->{Success} $mapA{$Brand_Name}->{Failure} \n";
}
#/usr/bin/perl
使用Text::CSV;
使用POSIXQW(strftime);
使用数据::转储程序;
使用LWP::Simple;
my$APK_GCM=“/root/Basavaraj/GCM/cdr_02-01-2018_StreamzGcm.csv”;
my$WEB_GCM=“/root/Basavaraj/GCM/cdr_02-01-2018_StreamzWebPushNotification.csv”;
my$Dayed=strftime(“%d-%m-%Y”,localtime(time-86400));
我的$Current_Date=strftime(“%d-%m-%Y”,localtime(time));
打印“$昨天\n”;
打印“$Current\u Date\n”;
打开(STDOUT,“>”,“/root/Basavaraj/STREAMZ_GCM_APK.txt”);
#创建类以按逗号逐行拆分文档,
我的$csv=Text::csv->new({sep_char=>','});
打开(我的$data,代码看起来很混乱,也不清楚,但我在这里发布了增加计数的方法,希望它能有所帮助,在您的机器中复制并粘贴下面的程序并进行尝试(演示如何增加计数,与上面提到的场景非常类似)
请花点时间回答您的问题,并将这段文字转换为带有标点符号的实际问题。这很难阅读。如果代码缩进正确,我们也可以阅读。您可能希望提供示例数据。@simbabque:令人震惊的是,OP是一个软件QA工程师,您缺少使用strict;使用warnings;
。总是从这些开始。!/usr/bin/perl w
?这不是更好。你仍然有有效的!我的%results;
,中间有一条注释,你不应该首先使用-w
(我们从2000年起就有使用警告;
),而且你缺少使用严格的
(自1994年起提供)。谢谢你抽出时间来纠正这一点,我还没有决定如何在这里投票。我想你是想向OP解释一个概念,你的代码有很好的文档记录和结构,但我缺少使用警告
的布拉格语,我真的不确定这是否真的回答了问题。我不理解这个问题n、 所以我不想回答而不是猜测。我不想在这里投票。我想打印每个品牌的成功计数和失败计数,但当尝试不打印时。你建议将品牌添加到哈希中,但我正在阅读行,并根据逗号分隔将其拆分,我想根据再来一份
#!/usr/bin/perl
use strict;
use warnings;
my %results;
%results = ('Brand A' => {'Success' => 0, 'Failure' => 0},
'Brand B' => {'Success' => 1, 'Failure' => 1});
# add new entry into existing hash
#
$results{'Brand C'}{'Success'} = 5;
$results{'Brand C'}{'Failure'} = 6;
# increment Success count for specific brand straightaway
$results{'Brand B'}{'Success'}++;
print "Success count for brand B = $results{'Brand B'}{'Success'}\n";
#print out hash
#
# first key for example Brand A
for my $brand (keys %results)
{
print "Printing brand here: $brand-->";
# next key for example 'Success' or 'Failure'
#
for my $result (keys %{$results{$brand}})
{
#increment success or failure count
$results{$brand}{$result}++;
print "$result-->$results{$brand}{$result},";
}
print "\n";
}