jeudi 23 mars 2017

how can I handle multiple 2D values in perl

I am working with a Perl script where I need to work with MTA's logs. Below is the query which I want to work with.

sh-3.2# cat /var/log/pmta/File_name-2017-03-23*|egrep 'email.domain.com'|cut -d, -f6|cut -d- -f1|sort|uniq -c [output of this query is stores in $case8Q1 ]

   310 blk
  1279 hrd
    87 sft
144056 success
    18 unk

As you can see above query gives 5 values but this is not always the case. it can also give like this. means the number of rows may vary each time (2 or 3 or 4 or max 5)

   310 blk
144056 success
    18 unk

below is the sample code which gives the wrong result

sub get_stats{
    $case8Q1 =~ s/^\s+//;
    @case8Q1_split = split ('\n', $case8Q1);
    @first_part = split (' ', $case8Q1_split[0]);
    @second_part = split (' ', $case8Q1_split[1]);
    @third_part = split (' ', $case8Q1_split[2]);
    @fourth_part = split (' ', $case8Q1_split[3]);
    @fifth_part = split (' ', $case8Q1_split[4]);

           if($first_part[1] eq 'blk'){
                    $report{Block} = $first_part[0];
            }
            elsif($first_part[1] eq 'hrd'){
                    $report{Hard} = $first_part[0];
            }
            elsif($first_part[1] eq 'sft'){
                    $report{Soft} = $first_part[0];
            }
            elsif($first_part[1] eq 'success'){
                    $report{Success} = $first_part[0];
            }
            elsif($first_part[1] eq 'unk'){
                    $report{Unknown} = $first_part[0];
            }

rest ifelse blocks so on........!

}

where report is a hash (%report).

Can some please help me how can operate it from here.

I have all the values but if I go with normal if-else like above it will take at least 25 if else blocks.

Let me know please if this is not clear.

Aucun commentaire:

Enregistrer un commentaire