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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: If conditional checks
by kitkit201 (Initiate) on Dec 13, 2012 at 00:16 UTC | |
by Kenosis (Priest) on Dec 13, 2012 at 00:48 UTC |
In Section
Seekers of Perl Wisdom