CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
variables in regular expressions

 



jknash
stranger

Jul 6, 2001, 10:05 AM

Post #1 of 4 (4941 views)
variables in regular expressions Can't Post

I am trying to use 2 variables in my regex and I can't get it to work. I can never get it to evaluate to true. Here's a code snippet ------>

#!/usr/bin/perl

%hash = (data => "172.31.1.1");


$entry = "172.31.1.1_se0.1.html";


if ($entry =~ m/%hash->{'data'}+/) {

print "Match\!\n";

}

Can someone please tell me what I'm doing wrong?

thanks for your help



randor
User

Jul 6, 2001, 10:45 AM

Post #2 of 4 (4937 views)
Re: variables in regular expressions [In reply to] Can't Post

jknash,

replace:

if ($entry =~ m/%hash->{'data'}+/) {

with:

if ($entry =~ m/$hash{'data'}+/) {



I Hope this helps...

perl programmers don't die.. they just start writing a new script.


jknash
stranger

Jul 6, 2001, 12:15 PM

Post #3 of 4 (4935 views)
Re: variables in regular expressions [In reply to] Can't Post

I tried that and now I get a match every time,,,whethere the hash is included in my script or not. I am running a program called mrtg to monitor our routers which outputs some html files for each router into a predefined directory. I want to do a couple of things with the html files. The html files are named the same - 172.30.12.1.se0.1.html is a good example. I want to parse the ip address out of the filename and compare it to a record in a table in the mysql database. If it's found in the database, I want to query and see if the ip(172.30.12.1) and interface(se0.1) exist in the interfaces table. So basically I need to setup the query, execute it, read the path where the file should be, if it's there parse the filename and put the ip/interface into the interfaces table. That shouldn't be too hard with perl, right? :-)

thanks for all of your help



mhx
Enthusiast

Jul 6, 2001, 12:58 PM

Post #4 of 4 (4934 views)
Re: variables in regular expressions [In reply to] Can't Post

Hi,

I've commented the code, so it shouldn't be hard to understand.

Code
#!/usr/bin/perl -w 
use strict;
use warnings;

# Use the following to see what the
# regex engine is actually doing...
use re 'debug';

my $entry = "172.31.1.1_se0.1.html";

my %hash = (data => "172.31.1.1");

# All strings that are about to be
# interpolated into a regex should
# be quotemeta'd before. Otherwise,
# the dots will match any character!
$_ = quotemeta foreach values %hash;

# Now, we want to check if $entry
# begins with $hash{data}...
if( $entry =~ /^$hash{data}/ ) {
print "Match!\n";
}

If you have any specific questions, feel free to ask. Hope this helps.

-- Marcus


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives