in reply to Re^3: 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've only just had chance to take a look at this, and this is almost certainly what I've been looking for - the output is perfect, IF I can pass the command output through to _DATA_

The commands output is ALWAYS the same formatting, different data so in theory this should work? If so, can you explain a way for me to pass the commands output over to _DATA_ for unpack?

  • Comment on Re^4: Turning regex capture group variables into arrays, then counting the number of objects in the array

Replies are listed 'Best First'.
Re^5: Turning regex capture group variables into arrays, then counting the number of objects in the array
by poj (Abbot) on Dec 06, 2018 at 10:25 UTC

    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

      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.

        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