Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Regex is not working

by Rahul Gupta (Sexton)
on Oct 03, 2013 at 04:56 UTC ( #1056719=perlquestion: print w/ replies, xml ) Need Help??
Rahul Gupta has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to substitute values in query but things are not working for me.

Here is my code

my @array_1 = ("\$Id_run_query=1", "\$name_run_query=xyz", "db_query_r +un_query=select * from meter where metetid=\$Id and metername=\$name" +); my $query; foreach(@array_1){ if ($_ =~ m/^db_query_run_query=(.*)/i){$query = $1;print "$1\n";} } foreach(@array_1){ if ($_ =~ m/^(\$\S+)=(.*)/i){ my $replace = $2; my $value = shift @{ [ split( /_/, $_ ) ] }; print "$value\n"; chomp($value); # print "$query\n"; $query =~ s/$value/$replace/i; } } print "===>$query\n";

the expected output is :

"select * from meter where metetid=1 and metername=xyz"

but i am geeting this:

"select * from meter where metetid=$Id and metername=$name"

Thanks in advance

Comment on Regex is not working
Select or Download Code
Re: Regex is not working
by moritz (Cardinal) on Oct 03, 2013 at 05:21 UTC
    $query =~ s/$value/$replace/i;

    The problem is that $value contains something like $Id, and the $ when interpreted as a regex means "match the end of the string", not "match a dollar sign". You can fix that by writing

    s/\Q$value\E/$replace/i;

    instead.

    Fwiw this is how I'd solve the same task:

    my @array_1 = ("\$Id_run_query=1", "\$name_run_query=xyz", "db_query_r +un_query=select * from meter where metetid=\$Id and metername=\$name" +); my $query; my %replace; foreach(@array_1){ if (m/^db_query_run_query=(.*)/i){ $query = $1 } elsif (/(\$\S+)_run_query=(.*)/) { $replace{$1} = $2; } } my $replace_re = join '|', map quotemeta, keys %replace; $query =~ s/($replace_re)/$replace{$1}/ig; print "===>$query\n";

    The call to quotemeta does the same as \Q...\E in the regex.

      Thanks it worked for me:-)

Re: Regex is not working
by kcott (Abbot) on Oct 03, 2013 at 06:18 UTC

    G'day Rahul Gupta,

    Your underlying problem is that the value of $value in $query =~ s/$value/$replace/i is being interpolated: $Id and $name evaluate to zero-length strings so $query =~ s/$value/$replace/i is equivalent to $query =~ s//$replace/i (i.e. no substitutions occur). That was a poor guess on my part; see moritz' explanation instead; the fixes, however, are identical. To fix, use

    $query =~ s/\Q$value\E/$replace/i;

    -- Ken

Re: Regex is not working
by CountZero (Bishop) on Oct 03, 2013 at 16:06 UTC
    If this query is to be used in a DBI environment to get data out of the database, wouldn't it be much safer (and probably easier too) to simply use "?" placeholders?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056719]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2014-07-23 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (133 votes), past polls