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

Very frustrated with what I believe is a simple loop. Working Loop: lacts - passed in hash with status of backups + if status is /Completed$/ then skip lyester - passed in date to compare to backup date + if backup date older than lyester then skip
sub check_activity { # check activity - Exceptions, Failures, Active, Queued, Repli +cation ## defaults ## my $lresults = {}; my $WEEK = time - (7 * 24) * 3600; # time a week ago... ## passed ## my $lacts = shift; my $lyester = shift; my $act; print "here in check activity\n"; #print "keys - " . join(',', keys %{$lacts}) . "\n"; foreach $act ( keys %{$lacts} ) { print "$act - $lacts->{$act}->{Status} - $lacts->{$act +}->{Type} - $lacts->{$act}->{StartTime} - $lyester\n"; } return $lresults; } OUTPUT: here in check activity cut....cut 9132184440017109 - Completed w/Exception(s) - Scheduled Backup - 2011- +11-20 19:00 PST - 2011-11-22 14:54:12 9132192720051109 - Completed - Scheduled Backup - 2011-11-21 18:17 PST + - 2011-11-22 14:54:12 9132201360045609 - Completed - Scheduled Backup - 2011-11-22 18:05 PST + - 2011-11-22 14:54:12 1321898408398257 - Completed - Replication Source - 2011-11-21 10:00 P +ST - 2011-11-22 14:54:12 9132192720002809 - Timed Out - Start - Scheduled Backup - 2011-11-21 1 +8:00 PST - 2011-11-22 14:54:12 cut...cut
The loop works, however the checks have not been added. If I add a next if, or if statement it will make a single loop and exit the sub. Broken code - only performs single loop.
sub check_activity { # check activity - Exceptions, Failures, Active, Queued, Replicati +on ## defaults ## my $lresults = {}; my $WEEK = time - ( 7 * 24 ) * 3600; # time a week ago... ## passed ## my $lacts = shift; my $lyester = shift; my $act; print "here in check activity\n"; #print "keys - " . join(',', keys %{$lacts}) . "\n"; foreach $act ( keys %{$lacts} ) { print $act . " - " . $lacts->{$act}->{Status} . "\n"; next if $lacts->{$act}->{Status} =~ /Completed$/; print "$act - $lacts->{$act}->{Status} - $lacts->{$act +}->{Type} - $lacts->{$act}->{StartTime} - $lyester\n"; } return $lresults; } OUTPUT: here in check activity 9132201720006509 - Completed
Any thoughts or advice is greatly appreciated. - Chuck

Replies are listed 'Best First'.
Re: Adding "if" statement to foreach causes only 1 run
by choroba (Cardinal) on Nov 23, 2011 at 23:44 UTC
    I do not get it. What do you think you have in $lacts->{$act}->{Status}? You can use Data::Dumper to see.

    You can store only a scalar in a hash value, so if you assign 'Completed' to $h->{Status}, the previous value of $h->{Status} is overwritten. To keep several statuses under one key, you have to use array reference, i.e. push @{ $h->{Status} }, 'Completed'.

Re: Adding "if" statement to foreach causes only 1 run
by Anonymous Monk on Nov 23, 2011 at 23:48 UTC

    Your updated subroutine, as written should, produce

    9132184440017109 - Completed w/Exception(s) 9132184440017109 - Completed w/Exception(s) - Scheduled Backup - 2011- +11-20 19:00 PST - 2011-11-22 14:54:12 9132192720051109 - Completed 9132201360045609 - Completed 1321898408398257 - Completed 9132192720002809 - Timed Out 9132192720002809 - Timed Out - Start - Scheduled Backup - 2011-11-21 1 +8:00 PST - 2011-11-22 14:54:12

    You need to post a Short, Self Contained, Correct Examples , see How do I post a question effectively?