Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^3: If conditional checks

by Kenosis (Priest)
on Dec 09, 2012 at 05:45 UTC ( #1007953=note: print w/replies, xml ) Need Help??

in reply to Re^2: If conditional checks
in thread If conditional checks

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.

Replies are listed 'Best First'.
Re^4: If conditional checks
by kitkit201 (Initiate) on Dec 12, 2012 at 20:13 UTC
    Thank you for the comment. One more thing about this is the host string. I see the host scalar to only work on the hostname if it only with db, but no other characters behind it, like db4323, how would you include it to have any other characters like periods and numbers, behind it? some example hostnames would be

      You're most welcome!

      Here's the regex in the grep (with two added features that should be there):

      /^\Q$host\E$/; ^ ^ ^^ | | || | | |+ - End of line | | + - End quote metacharacters | + - Begin quote metacharacters (e.g., [.*\+], etc.) + - Match beginning at the start of the line

      If you remove the trailing $, it'll match a host name beginning with "db". Try the following with and without the trailing $ in the regex:

      use warnings; use strict; my $host = 'db'; while (<DATA>) { print if /^\Q$host\E$/i; } __DATA__ db db4323

      Output with $:


      This is because if forces an exact match, like $host eq 'db'.

      Output without $:

      db db4323

      This is becuase it's only matching the first two characters of $_ (the default scalar) against the value of $host.

      Note that there's the i modifier at the end of the regex. This makes the match case-insensitive, in case you have a host like DB4323.

        Hi Kenosis, So here is the code that I have that is not working with what you did, maybe it's just me being silly again..
        #!/usr/local/bin/perl use strict; use warnings; use List::Util qw/sum/; my $host = ''; 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 ( grep /^\Q$host\E/i, @redAlert or ( grep /^$host/i, @orangeAlert and $statusHist01 == 2 ) or $statusHistTot == 3 ) { print "We've got a winner!"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007953]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-05-21 19:38 GMT
Find Nodes?
    Voting Booth?