Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

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

        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'
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1007935]
Approved by tobyink
[stevieb]: yep. That's definitely the issue. Unzipping the file manually in explorer extracts the whole thing properly

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2017-03-28 16:40 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (336 votes). Check out past polls.