Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

multiple substitutions

by Loose Moose (Initiate)
on Apr 10, 2002 at 04:44 UTC ( #157955=perlquestion: print w/replies, xml ) Need Help??

Loose Moose has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a long string full of HREF's for links that map things over a .GIF image of a visio diagram. I have a need to modify the HREF's when the image is displayed, but I only need to change the ones that fit that match below. There are often (not always) many matches in the string. For each match, $1 will be different. However, I need to take $1 from the match, use it to get more info from the database and use the result in a substitution at that point.
if ($$imageMap =~ /name=(.*?)_DIAGLINK/){ $diagId = $1; $dbh = ConnectDB($User, $Password); $sql = "SELECT version FROM diagramversion WHERE diagram_id='$ +diagId' AND workflow_state='Stored'"; #to_char(version,'099') didn't + work $sth = RunJobDB($dbh, $sql); @version = $sth->fetchrow_array; $version = $version[0]; $sth->finish(); DisconnectDB($dbh); $version = sprintf( "%03d", $version ); $fullId = $diagId."-".$version; $$imageMap =~ s/ViewObject.pl\?name=(.*?)_DIAGLINK/dia_View.pl +?diagram_id=$fullId/i; }
How can I do this so that it works properly for each substitution ??

Replies are listed 'Best First'.
Re: multiple substitutions
by IndyZ (Friar) on Apr 10, 2002 at 05:12 UTC
    Try the global match (g) and eval (e) flags. Here's an example:
    #!/usr/local/bin/perl while (<>) { s/([A-Z]+)/reverse($1)/ge; print; }
    The first part is obvious: It matches 1 or more capital characters in the range A-Z, and grabs it to $1. The 'e' flag on the regexp causes the the 'reverse($1)' to be eval'd and the return value to be substituted in, and the 'g' flag makes as many matches as possible. This particular example reverses any and all sections of capital letters so "LREP java PYTHON" would become "PERL java NOHTYP".

    Instead of using the reverse function, write your own function to do the database access, pass it the matched data and have it return what should be substituted in.

    Just a warning: I haven't run any benchmarks or anything, but this will probably cause a pretty big speed hit since you are running an eval() on every match.

    Update:
    Here are my untested changes to your code. It may or may not work.

    sub getfromdb { $diagId = shift; $dbh = ConnectDB($User, $Password); $sql = "SELECT version FROM diagramversion WHERE diagram_id='$ +diagId' AND workflow_state='Stored'"; #to_char(version,'099') didn't + work $sth = RunJobDB($dbh, $sql); @version = $sth->fetchrow_array; $version = $version[0]; $sth->finish(); DisconnectDB($dbh); $version = sprintf( "%03d", $version ); $fullId = $diagId."-".$version; return("dia_View.pl+?diagram_id=$fullId"); } $$imageMap =~ s/ViewObject.pl\?name=(.*?)_DIAGLINK/getfromdb($1)/gei;

    Yet Another Update:
    Variable names like '$$imageMap' are very difficult to maintain and it's almost impossible to make sure you don't walk all over important variables. Try using a hash instead.

    --
    IndyZ

      <quote> $$imageMap =~ s/ViewObject.pl\?name=(.*?)_DIAGLINK/getfromdb($1)/gxi; </quote>
      - - - - - - - - - - - - - - - - - - - -

      Shouldn't there be an "e" in the options?

      $$imageMap =~ s/ViewObject.pl\?name=(.*?)_DIAGLINK/getfromdb($1)/gxie;
      Otherwise, you'll be replacing with a literal "getfromdb(whateverdollar1matched)", I think...
      --
      Mike
        Yep, you're right. This is what I get for typing on no sleep. The example has been fixed.

        --
        IndyZ
Re: multiple substitutions
by belg4mit (Prior) on Apr 10, 2002 at 05:05 UTC
    If I understand you correctly, try making that if a while.

    UPDATE: Doh! ++ For IndyZ and RMGir. There I go ignoring details again...

    --
    perl -pe "s/\b;([mnst])/'\1/mg"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-09-30 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (125 votes). Check out past polls.

    Notices?