Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^5: If conditional checks

by Kenosis (Priest)
on Dec 12, 2012 at 21:46 UTC ( #1008595=note: print w/ replies, xml ) Need Help??


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

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 dbwindows822.tp.com db726.tp.com

Output with $:

db

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

Output without $:

db db4323 dbwindows822.tp.com db726.tp.com

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.


Comment on Re^5: If conditional checks
Select or Download Code
Replies are listed 'Best First'.
Re^6: If conditional checks
by kitkit201 (Initiate) on Dec 13, 2012 at 00:16 UTC
    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 = 'db201.tp.mud'; 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!"; }

      Ah! We just need to do something a little different with the partial match:

      #!/usr/local/bin/perl use strict; use warnings; use List::Util qw/sum/; my $host = 'db201.tp.mud'; 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 $host =~ /^$_/i, @redAlert or ( grep $host =~ /^$_/i, @orangeAlert and $statusHist01 == 2 ) or $statusHistTot == 3 ) { print "We've got a winner!"; }

      The earlier grep worked because we were looking for an exact match. In this case, however, we want a partial match of the alert elements with the host. Also, since the alerts don't contain any 'special' characters, escaping isn't necessary.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008595]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2015-08-01 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls