regex find then replace - a cleaner approach?

Discussion in 'Perl Misc' started by John, Dec 11, 2009.

  1. John

    John Guest

    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
     
    John, Dec 11, 2009
    #1
    1. Advertising

  2. John

    J. Gleixner Guest

    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=?";
     
    J. Gleixner, Dec 11, 2009
    #2
    1. Advertising

  3. John

    John Guest

    "J. Gleixner" <> wrote in message
    news:4b2271b5$0$87079$...
    > 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
     
    John, Dec 11, 2009
    #3
  4. "John" <> 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
     
    Jürgen Exner, Dec 11, 2009
    #4
  5. John

    patrick Guest

    Re: regex find then replace - a cleaner approach?

    On Dec 11, 7:42 am, "John" <> 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
     
    patrick, Dec 14, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    313
  2. rh
    Replies:
    6
    Views:
    133
    Nick Mellor
    Feb 8, 2013
  3. Demian Brecht
    Replies:
    18
    Views:
    244
  4. Demian Brecht
    Replies:
    0
    Views:
    138
    Demian Brecht
    Feb 6, 2013
  5. MRAB
    Replies:
    0
    Views:
    116
Loading...

Share This Page