in reply to Re^4: Turning regex capture group variables into arrays, then counting the number of objects in the array
in thread Turning regex capture group variables into arrays, then counting the number of objects in the array

I used the filehandle DATA as a convenient way to include some test lines in a Simple, Self Contained Example. You can either do as markong suggested and populate an array with your output

my @output = `bpdbjobs`; #while (<DATA>){ for (@output) { # process lines }

or if you have the output in a text file, open a filehandle to that

my $filename = 'output.txt'; open my $fh,'<',$filename or die "Could not open $filename : $!"; while (<$fh>){ # process lines }

or you may be able to pipe the output directly from your program. see Calling External Commands More Safely

open my $fh, '-|', 'bpdbjobs' or die $!; while (<$fh>){ # process lines } close $fh or die $! ? $! : $?;
Updated ; last example corrected backticks to single quote
poj
  • Comment on Re^5: Turning regex capture group variables into arrays, then counting the number of objects in the array
  • Select or Download Code

Replies are listed 'Best First'.
Re^6: Turning regex capture group variables into arrays, then counting the number of objects in the array
by Djay (Novice) on Dec 06, 2018 at 11:20 UTC

    I figured that one out after I realised DATA was a handle. My mistake, I'm new to this language :D

    One final thing, as I've tested and tweaked  my %fmt and confirmed I can get this working.

    I have the following outputs

    printf "Statistic.SUCCESSFULL = %d\n",$counts{'Status'}{'0'}; printf "Statistic.PARTIAL = %d\n",$counts{'Status'}{'1'}; printf "Statistic.FAILED = %d\n",$counts{'Status'}{''};

    However I am struggling to find a way to count the failed backups, as failed backups have a status code between 2 and 9999. In Powershell this would be a simple  -gt '1' or to be more specific.

     $PolicyFails = $ColumnName | where {$_.Status -ne $null -and $_.Status -ne "0" -and $_.Status -ne "1"}

    I have tried doing

     printf "Statistic.FAILED = %d\n",$counts{'Status'}{'>1'};

    but this does not work, I assume there is somewhere else I can put the gt or > to tell perl I want to count all numbers in the status column greater than 1?

      Here's one approach which looks complex but you can break it down into its constituent parts easily enough.

      #!/usr/bin/env perl use strict; use warnings; my %counts = ( Status => { 0 => 99, 1 => 12, 2 => 3, 77 => 4 } # Other keys would go here ); my $totalfails = 0; $totalfails += $counts{Status}{$_} for grep {$_ > 1} keys %{$counts{St +atus}}; print "Total failures: $totalfails\n";

      Looking at the long line which does all the work, from "grep" to the end extracts the hash keys which match your criteria (>1). The "for" then iterates over these keys, assigning them to $_ each time. The "+=" extracts the value associated with each key from the original hash and cumulatively adds them to $totalfails. HTH.

        I believe its now working! Full script below, cobbled together thanks to the wonderful work of the Perl Monks!
        #!/usr/bin/perl use strict; use Data::Dumper ; my $str = `bpdbjobs`; my $filename = 'bpdbjobs.txt'; open(FH, '>', $filename) or die $!; print FH $str; close(FH); open(FH, '<', $filename) or die $!; my $fmt = 'A5 A15 A1 A7 A6'; my %counts = (); my @col = ('JobID','Col2','Type','State', 'Status','Policy','Schedule','Client', 'Dest Media Svr','Active PID'); # 10 cols my $totalfails = 0; while (<FH>){ next unless /\S/; # skip blank lines next if /^\s+JobID/; # skip header chomp; my @f = unpack $fmt,$_; s/^\s+|\s+$//g for @f; # trim spaces # count each column for my $n (0..$#col){ ++$counts{$col[$n]}{$f[$n]}; } print join "\|",@f,"\n"; # check } $totalfails += $counts{'Status'}{$_} for grep {$_ > 1} keys %{$counts{ +'Status'}}; print Dumper \%counts; printf "Statistic.SUCCESSFULL = %d\n",$counts{'Status'}{'0'}; printf "Statistic.PARTIAL = %d\n",$counts{'Status'}{'1'}; printf "Statistic.FAILS = $totalfails\n"; close(FH);

      You could use logic to create some more keys in the %counts hash. e.g

      # count each column for my $n (0..$#col){ ++$counts{$col[$n]}{$f[$n]}; if ($col[$n] eq 'Status'){ ++$counts{$col[$n]}{'FAILED'} if $f[$n] > 1; } } printf "Statistic.FAILED = %d\n",$counts{'Status'}{'FAILED'};
      poj