I would think, and have been trying :
$line =~ /\$myVariable/
If I do $line like this :
$line = 'HI $myVariable';
If($line =~ /\$myVariable/) { print "HI"; }
It works great, the problem is I can't do $line like that.
It treats line like I did it as
$line = "HI $myVariable";
Which is actually "HI whatevermyvariablecontains"
That's the dilemma.
-----Original Message-----
From: Rob Dixon [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, January 14, 2003 9:40 AM
To: [EMAIL PROTECTED]
Subject: Re: finding variable name in string
Hi Ben
You're not soing what you think you're doing. See below.
"Ben Siders" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> 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";
> }
>
This is the same as
if ( $line =~ /blorg/ ) { .. }
which will fail, since your data line doesn't contain that string.
>
> if ( $line =~ /$$myVariable/ ) {
> print "2. Line contains by variable.\n";
> }
>
This is that same as
if ( $line =~ /${$myVariable}/ ) { .. }
or
if ( $line =~ /$blorg/ ) { .. }
which doesn't exist, so evaluates to the empty string
if ( $line =~ // ) { .. }
Now every string is deemed to contain the empty string, and so your test succeeds.
It's nothing to do with having the text 'myVariable' in there.
>
> And here's the runlog:
>
> [bsiders@lysol perl]$ ./test.pl asdlkjasdkljmyVariableasdlkjasd
> 2. Line contains by variable.
> [bsiders@lysol perl]$
>
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]