Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: If conditional checks

by kitkit201 (Initiate)
on Dec 09, 2012 at 05:14 UTC ( #1007952=note: print w/replies, xml ) Need Help??


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

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

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

    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.

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

        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.

Re^3: If conditional checks
by tobyink (Abbot) on Dec 09, 2012 at 11:06 UTC

    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: note [id://1007952]
help
Chatterbox?
[choroba]: a "thinko"
[erix]: my app (google, really) makes that 1530 km...
[Eily]: google tells me 1662 km ...
[LanX]: Nancy? Somewhere near Metz ...
[Eily]: must be km farenheit or something :P
[Eily]: wow, don't say that in Nancy LanX :P
[erix]: maybe you used the car routes, I used the walking routes
[Eily]: there's a rivalry between the two cities, so defining one in terms of the other might not be well received
[Eily]: erix well I did click on the walking icon
LanX giggles!

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-12-13 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (369 votes). Check out past polls.

    Notices?