To expand, I wrote a brief sample program to figure this out, since I actually had no idea how to do it. Here it is:

#!/usr/bin/perl

$myVariable = "blorg";

$line = <STDIN>;

if ( $line =~ /$myVariable/ ) {
print "1. Line contains my variable.\n";
}

if ( $line =~ /$$myVariable/ ) {
print "2. Line contains by variable.\n";
}

And here's the runlog:

[bsiders@lysol perl]$ ./test.pl
asdlkjasdkljmyVariableasdlkjasd
2. Line contains by variable.
[bsiders@lysol perl]$

One thing I noticed is that it'll still incorrectly match the first case, so it's not a good solution after all. D'oh.

[bsiders@lysol perl]$ ./test.pl
blorg
1. Line contains my variable.
2. Line contains by variable.




Ben Siders wrote:

Use two $$.

if ( $line =~ /$$myVariable/ ) {
   &doSomethingSpiffy;
}

Dan Muey wrote:

If I do that won't it look for the 'value' of $variable?
I need it to find the actual string '$variable' not what $variable contains.
Any other ideas?

-----Original Message-----
From: Paul Johnson [mailto:[EMAIL PROTECTED]] Sent: Tuesday, January 14, 2003 9:12 AM
To: Dan Muey
Cc: Jenda Krynicky; [EMAIL PROTECTED]
Subject: RE: finding variable name in string



Dan Muey said:


To simplify the question and not get off track I need to see if a string contains a variable 'name' not 'value'.

If($string =~ m/\$variable/) { print "Yes \$variable exists"; }

Don't escape the $ in the RE.





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to