var within reg expression

K

kielhd

Hi NG,

I want to search the keys of the hash %z for the value of $x. If $x
matches $i (line 9), a string should be printed.
$x matches key "Hallo", but there is no output!
Where is my mistake?

Thanks in advance
Henning


01 #!C:/Perl/bin/perl.exe -w
02 $x = "all";
03 %z = (
04 "Ich" => "1",
05 "Du" => "2",
06 "Hallo" => "3"
07 );
08 foreach $i ( keys %z ) {
09 if ( $x =~ /$i/ ) { print "$x is part of $i ...\n"; }
10 }
 
J

John Strauss

On 29 Jul 2003 02:25:34 -0700
Hi NG,

I want to search the keys of the hash %z for the value of $x. If $x
matches $i (line 9), a string should be printed.
$x matches key "Hallo", but there is no output!
Where is my mistake?

Thanks in advance
Henning


01 #!C:/Perl/bin/perl.exe -w
02 $x = "all";
03 %z = (
04 "Ich" => "1",
05 "Du" => "2",
06 "Hallo" => "3"
07 );
08 foreach $i ( keys %z ) {
09 if ( $x =~ /$i/ ) { print "$x is part of $i ...\n"; }
10 }

your mistake is on line 09, where you search $x for pattern $i.
you want to search $i for pattern $x, so change your if condition
to "if ( $i =~ /$i/ )" ...


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail
 
G

Gunnar Hjalmarsson

kielhd said:
I want to search the keys of the hash %z for the value of $x. If $x
matches $i (line 9), a string should be printed.
$x matches key "Hallo", but there is no output!
Where is my mistake?

09 if ( $x =~ /$i/ ) { print "$x is part of $i ...\n"; }

It should be the other way around:

if ( $i =~ /$x/ )
----------^------^
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (kielhd) wrote in @posting.google.com:
01 #!C:/Perl/bin/perl.exe -w
02 $x = "all";
03 %z = (
04 "Ich" => "1",
05 "Du" => "2",
06 "Hallo" => "3"
07 );
08 foreach $i ( keys %z ) {
09 if ( $x =~ /$i/ ) { print "$x is part of $i ...\n"; }
10 }

A couple of style comments:

Consider using more meaningful variable names.

Consider using index() instead of a pattern match.

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPyZBS2PeouIeTNHoEQIKOQCeOMFpHNTqE9nKoxt18eX/QmvKzYkAni37
9UhDOKHu57ULFKXswxoXj7CU
=Mf0y
-----END PGP SIGNATURE-----
 
N

news

you want to search $i for pattern $x, so change your if condition
to "if ( $i =~ /$i/ )" ...

Remember that if $i contains special characters (those recognised by
the RE engine) it will have unexpected consequences.

You may prefer to use this,

if ($x =~ /\Q$i/) ...

Chris
 
G

Gunnar Hjalmarsson

Remember that if $i contains special characters (those recognised
by the RE engine) it will have unexpected consequences.

You may prefer to use this,

if ($x =~ /\Q$i/) ...

This is getting confusing. John made a typo, and Chris illustrated his
point using the original incorrect expression. The pattern is $x:

if ($i =~ /\Q$x/)
 
J

John Strauss

This is getting confusing. John made a typo, and Chris illustrated his
point using the original incorrect expression.

Sorry! You're quite correct

Chris[/QUOTE]


sorry for the confusion. (un)happy fingers.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,121
Messages
2,570,712
Members
47,283
Latest member
hopkins1988

Latest Threads

Top