Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regex find then replace - a cleaner approach?

Reply
Thread Tools

regex find then replace - a cleaner approach?

 
 
John
Guest
Posts: n/a
 
      12-11-2009
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





 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      12-11-2009
John wrote:
> 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=?";
 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      12-11-2009

"J. Gleixner" <(E-Mail Removed)> wrote in message
news:4b2271b5$0$87079$(E-Mail Removed)...
> John wrote:
>> 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=?";


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



 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-11-2009
"John" <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
patrick
Guest
Posts: n/a
 
      12-14-2009
On Dec 11, 7:42*am, "John" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
greater then / less then =?Utf-8?B?TWlrZQ==?= ASP .Net 2 11-04-2004 06:05 PM



Advertisments