rajsai28 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Expert,

I am executing unix command 'gunzip -c test.txt.gz |cut -f3 -d'|'|sort|uniq -c' and stroing the value in a variable, the zip file contains records as shown below:

ABC|123|CHECK|1| DEF|456|CHECK|1| GHI|789|CHECK|1| ABC|123|CHECK|1| DEF|456|CHECK|1| GHI|789|CHECK|1| ABC|123|CHECK|1| DEF|456|KCEHC|1| GHI|789|KCEHC|1| JKL|101|KCEHC|2|

I have used the code as shown below, i am executing the unix commands and storing the value in a variable and from the results i am fetching only numeric value

#!/usr/bin/env perl use warnings; use strict; use IO::Zlib; my $fh = IO::Zlib->new('test.txt.gz', 'rb') or die "Zlib failed: $!"; my %count; $count{$_}++ for map { (split /\|/)[2] } <$fh>; foreach $key(%count){ $c = $count{$key}; $c1 = $count{$key}; }

gives me this output:

<ocde> $c = 4 $c1 = 4 </code>

The output of dumper

$VAR1 = 'CHECK'; $VAR2 = 7; $VAR3 = 'KCHEC'; $VAR4 = 4;

I also tired to grep the CHECK - $r = grep /CHECK/, $r, it returns a 1

i want store the value as CHECK= 7 and KCHEC = 4, separately

Experts any help on this, thanks,regards

I dont want to hard code the values such as 'CHECK' and 'KCHEC', becuase if the record contains other value like CHECK1 and KCHEC, then this will not work

$c=$count{'CHECK'}; $c1=$count{'KCHEC'};

Replies are listed 'Best First'.
Re: How to reterive the values from hashes
by NetWallah (Canon) on Jul 03, 2013 at 02:50 UTC
    Your DUMPER output indicates that %count does contain the information you expect.

    The problem is how you are extracting it.

    Both $c and $c1 are being set to the same value, in your code "$count{$key}".
    Instead, try:

    foreach my $key(%count){ my $c = $key; my $c1 = $count{$key}; print "$c = $c1\n"; # Or more directly, print "$key = $count{$key}\n"; }

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: How to reterive the values from hashes
by poj (Abbot) on Jul 03, 2013 at 06:59 UTC

    If you are sure you will only ever have 2 keys, and only one of them will have the word CHECK within it try

    foreach $key (%count){ if ($key =~ /CHECK/){ $c = $count{$key}; } else { $c1 = $count{$key}; } } print "c = c$ , c1 = $c1\n"
    poj