哈希键值打印问题

时间:2018-01-21 14:29:26

标签: 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_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";
}

1 个答案:

答案 0 :(得分:-2)

代码看起来很乱并且不清楚,但是我在这里发布增加这种情况的计数的方法,希望它会有所帮助,将下面的程序复制并粘贴到你的机器中并试用它(演示如何增加数,非常类似于上面提到的情景)

#!/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";
}