哈希键值打印问题



我没有计数$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: $linen";
  }
}
#$, = ",";
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
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";
}

最新更新