Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

If conditional checks

by kitkit201 (Initiate)
on Dec 08, 2012 at 23:02 UTC ( #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

Comment on If conditional checks
Download Code
Re: If conditional checks
by rpnoble419 (Pilgrim) on Dec 09, 2012 at 00:33 UTC

    Have you looked into the Switch statement?

        Weren't there a whole pile of warnings about using source filters at one point?
        Yes. The Switch module mistakenly escaped into Perl 5.8. Do not use it. The Switch module was deprecated in Perl 5.10 and removed in Perl 5.14.

        Notice that in Categorized Damian Modules, the Switch module is categorized by its author as "Damian modules you shouldn't use in production because their purpose is to explore and prototype future core language features".

        See also: stackoverflow question on why the Switch module is deprecated.

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

        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.

        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'

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-12-20 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls