Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

If conditional checks

by kitkit201 (Initiate)
on Dec 08, 2012 at 23:02 UTC ( [id://1007935]=perlquestion: print w/replies, xml ) Need Help??

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

I want to do some conditional checks with respect to hostname and the following status history hash as shown below.. $host is the input variable . I want the IF condition to either check if the host is a red-type-alert, or and orange-type-alert with the first two status_history to be 1,1 or any other alert with status_history be 1,1,1 Here's what I have now..
@redAlert = ( "db" ); @orangeAlert = ("c", "sm" ); 'status_history' => { 'status' => [ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 ], if ( ( $redAlert =~ $host ) || ( $orangeAlert =~ $host && $status_his +tory =~ /1,1/) || ( $status_history =~ /1,1,1/ ) )
Would that work? Thanks kit

Replies are listed 'Best First'.
Re: If conditional checks
by Kenosis (Priest) on Dec 09, 2012 at 00:59 UTC

    Here's one way to do it, using the same logic you've used:

    use strict; use warnings; use List::Util qw/sum/; my $host = 'db'; my @redAlert = ("db"); my @orangeAlert = ( "c", "sm" ); my %status = ( 'status_history' => { 'status' => [ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 ] + } ); my $statusHist01 = sum @{ $status{status_history}{status} }[ 0 .. 1 ] +; my $statusHistTot = sum @{ $status{status_history}{status} }; if ( $host ~~ @redAlert or ( $host ~~ @orangeAlert and $statusHist01 == 2 ) or $statusHistTot == 3 ) { print "We've got a winner!"; }

    The alerts are contained in arrays, so Perl's (v5.10+) smart operator (~~) can be used to check whether the value of $host is an element of either array. List::Util's sum is used to sum the relevant values from the status list.

      Thanks Superdoc, but I dont have perl 5.10 installed on my production system. is there any other way to do it? without the smart operator

        If you don't have Perl 5.10, you could try Perl 5.12, or Perl 5.14, or Perl 5.16. Or wait 6 months and try Perl 5.18.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        You can use grep instead of the smart match operator (~~):

        ... if ( grep /^$host$/, @redAlert or ( grep /^$host$/, @orangeAlert and $statusHist01 == 2 ) or $statusHistTot == 3 ) { print "We've got a winner!"; } ...

        grep iterates through all array elements, and in scalar context (as in this case), it returns the number of times an expression is true. Each expression in the greps is a regex that requires an exact match between the value of $host and an array element (contained in the default scalar, $_) to be evaluated as true.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1007935]
Approved by tobyink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-23 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found