- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl - using a scaler variable to represent an R.E...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2002 05:27 AM
03-11-2002 05:27 AM
perl - using a scaler variable to represent an R.E.
$pattern='/humpty|jack/'
.
.
if ($variable_x =~ $pattern)
{
print 'Match found for humpty or jack'
}
Does not work.
If instead of $pattern I use /$var_a|$var_b/ where $var_a = humpty and $var_b = jack then it works fine! What am I not getting???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2002 05:32 AM
03-11-2002 05:32 AM
Re: perl - using a scaler variable to represent an R.E.
$pattern=/humpty|jack/;
get rid of the quotes
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2002 06:23 AM
03-11-2002 06:23 AM
Re: perl - using a scaler variable to represent an R.E.
Therefore do the following-
$pattern='humpty|jack'
.
.
if ($variable_x =~ /$pattern/)
{
print 'Match found for humpty or jack'
}
Should work.
Note- If $pattern does not change, you can use the "o" option (ie /$pattern/o) to have perl generate more effecient code for execution.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2002 06:35 AM
03-11-2002 06:35 AM
Re: perl - using a scaler variable to represent an R.E.
I like your solution much better!
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2002 09:20 AM
03-11-2002 09:20 AM
Re: perl - using a scaler variable to represent an R.E.
$pattern = qr/humpty|jack/;
.
.
if ($variable_x =~ $pattern)
{
print 'Match found for humpty or jack'
}
Harry's solution is plain wrong: it'd asign the result of matching $_ against the pattern. Rodney's solution is technically correct, but still suboptimal and error-prone.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2002 09:41 AM
03-11-2002 09:41 AM
Re: perl - using a scaler variable to represent an R.E.
In deed you can use almost any character pair to embrace search and substituion patterns when using the operators m, s, y, tr
At the shell type
perldoc perlop
perldoc -f m
perldoc -f y
perldoc -f s
perldoc -f qr
This comes handy when you have to deal with paths, as subdirectories are delimited by /, which you otherwise would have to escape by
instead of writing
$path =~ /\/usr/\/local/\/bin/
you can say
$path =~ m|/usr/local/bin/|
or
$path =~ m(/usr/local/bin)
Of course you can also reference variables in patterns
@users = qw(Humpty Jack);
print "found match\n" if ($name =~ /$users[0]|$users[1]/)
You can even use the qr operator if you insist
$pattern = qr(Humpty|Jack);
print "found match" if $name =~ $pattern;
You can inverse logic by using '!~' instead of '=~'
But this should be familiar from awk.