Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

matching variable problem

Status
Not open for further replies.

denis60

Technical User
Apr 19, 2001
89
0
0
CA
Hi!

I'm trying to match a variable containing a $(dollar sign) and it fail, like

$tst="2->DATA$\n";
$tst1='2->DATA$';
if($tst =~ $tst1){print "yes\n"}else{print "no\n"};

These 2 variables are here to represent data capture in 2 files and compare.

How to fix it ???
Thanks in advance for your help
 
$tst="2->DATA$\n";
$tst1='2->DATA$';
if($tst eq $tst1){print "yes\n"}else{print "no\n"};
 
Sorry
The variable $tst could have more than 1 expression.
ex.: $tst="2->DATA$\n2->data1$\nn2->data2$\n";
 
I think that you have to escape the dollar sign because perl is interpreting the $ sign as a varible..

HTH - Stiddy
 
How can i do that when the $ is not always there.
Is it possible to have an exemple?
 
if you backslash the $ like this it will work
Code:
$tst="2->DATA\$\n2->data1\$\nn2->data2\$\n";
$tst1='2->DATA\$';          
if($tst =~ /$tst1/){print "yes\n"}else{print "no\n"};
but if you cannot backslash them for some reason, then it will Never work.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I receive text file containing file name with $ at time and i have to manipulte them.

Thanks for your help...
 
You should be using [tt]eq[/tt] rather than [tt]=~[/tt] if you are trying to match fixed strings. It's faster and you don't get the problem escaping characters.

If you are saying that $tst could contain a "\n"-separated list of values, then split them: it's still faster.
Code:
my @vals = split( "\n", $tst );
my @hits = grep { $tst1 eq $_ } @vals;
print @hits ? 'yes' : 'no';

This prints "yes" if $tst1 is one of the "lines" of $tst.

Yours,

fish

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top