regex find then replace - a cleaner approach?

J

John

Hi

I have

if ($SIC =~m |20745|) {
$SIC =~s |20745|567345|g;
$sql="UPDATE $table SET SIC='$SIC' WHERE id='$id'"
}

It works, but there must be a cleaner way without having to repeat the
'find' first and then the 'replace'. The $sql line only takes effect if
there is a successful match.

Regards
John
 
J

J. Gleixner

John said:
Hi

I have

if ($SIC =~m |20745|) {
$SIC =~s |20745|567345|g;
$sql="UPDATE $table SET SIC='$SIC' WHERE id='$id'"
}

It works, but there must be a cleaner way without having to repeat the
'find' first and then the 'replace'. The $sql line only takes effect if
there is a successful match.

There's nothing wrong with that, except possibly that
you're not using placeholders in your SQL. However, if
you really want to..


if ( $SIC =~ s/20745/567345/g )
{
my $sql = "UPDATE $table SET SIC=? WHERE id=?";
# do something with $sql..
}

or if you only want to do something with $sql, then
return/next right away:


return unless $SIC =~ s/20745/567345/g;
my $sql = "UPDATE $table SET SIC=? WHERE id=?";
 
J

John

J. Gleixner said:
There's nothing wrong with that, except possibly that
you're not using placeholders in your SQL. However, if
you really want to..


if ( $SIC =~ s/20745/567345/g )
{
my $sql = "UPDATE $table SET SIC=? WHERE id=?";
# do something with $sql..
}

or if you only want to do something with $sql, then
return/next right away:


return unless $SIC =~ s/20745/567345/g;
my $sql = "UPDATE $table SET SIC=? WHERE id=?";

Hi

OK. I had assumed the if (expression) was always boolean - if (X>6) etc - I
did not realise it tested whether an expression executed.

Regards
John
 
J

Jürgen Exner

John said:
OK. I had assumed the if (expression) was always boolean - if (X>6) etc -

It is
I did not realise it tested whether an expression executed.

It doesn't.

You are thourougly confused. Yes, the condition in an 'if' must be
boolean. So let's look at it in detail
if ( $SIC =~ s/20745/567345/g ){

What is the return value of s///? The documentation says

s/PATTERN/REPLACEMENT/msixpogce
[...] and returns the number of
substitutions made. Otherwise it returns false.

So, if there were no substitutions then s/// already returns false.
And if there were substitutions made then it returns a number larger
than 0. And the boolean value of any number larger then 0 is true.

So you get exactly the boolean value you would expect on success and
failure. This has nothing to do with if an expression was executed or
not but simply with a smart design for the return value of s///.

jue
 
P

patrick

Hi

I have

if ($SIC =~m |20745|) {
       $SIC =~s |20745|567345|g;
       $sql="UPDATE $table SET SIC='$SIC' WHERE id='$id'"

}
In Oracle you might just use (if doing the entire table):
update $table
set SIC = replace(SIC, '20745', '567345')
where SIC like '%20745%'

====>Pat
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top