in reply to Re^3: How to fetch the value of unix command result
in thread How to fetch the value of unix command result

I also tired the below one....

my $r = `gunzip -c $file|cut -f3 -d'|'|sort|uniq -c`; my $c1 = substr( $r, 16,-2); my $c2 = substr( $, 9,-9);

This gives me the output as...
<code> $c1 = 4 $c1 = 6 <code>

I have a question to experts, was this correct method

In a word, no. It might (and I emphasize, might) work for your current input. What happens, though, when:

Maybe you'll be ridiculously lucky and not ever encounter any of the above scenarios. But I guarantee, if you continue writing code that takes inherently variable width text input and parses it with fixed offsets, it will blow up horribly at some point.

Replies are listed 'Best First'.
Re^5: How to fetch the value of unix command result
by rajsai28 (Novice) on Jun 30, 2013 at 13:07 UTC
    Hi Expert,

    I have used your code and tested and i found the below output

    code

    #!/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>; printf "%-10s %4d\n", $_, $count{$_} for sort keys %count;

    The output which i got

    <ocde> CHECK 7 KCEHC 4 </code>

    Can i store them separately in $c and $c1 ? how can i print them individualy, how can i assign 4 to $c1 and 7 to $c1, please explain

      how can i assign 4 to $c1 and 7 to $c1

      Are you sure you mean that, or should one of those $c1s be $c ?

      poj
        Hi Expert,

        I mean 4 should be assigned to $c and 7 should be assigned to $c1

        Thanks