[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] regex help



On 11/27/99 at 1:31 AM, Nicholas G. Thornton wrote:

: (I have the feeling there'll be some linewrap promlems with this mail)
: I'm creating a droplet to correct an error made on many pages for work. 
: Unfortunately I cannot get it to work quite. At the very bottom of this mail is 
: the code in question (the rest of the droplet works fine). Basically there's an 
: image map at the tops of all the pages that currently looks like...
: 
: [snip]
: 
: 
:              open FILE, "<$file" or die "unable to open '$file'";
:                while (<FILE>) {
:                     if ($_ =~ /<IMG usemap="#map1" src="(.*)titlebar\.GIF" 
: width="467" height="60" border="0" align="top">/) {
:                          $bug = $1;
:                          $back = $bug =~ tr/\.\.\//\.\.\//;
:                          undef $bug;
:                     }
:                }
:                close FILE;


First, you're not using translate correctly.  You appear to want to count '../',
but in this case that is not what translate does.  When you use =~, translate
modifies $bug and assigns that modified string to $back, it does not return the
count.  In order to count using tr///, your string needs to be in $_ and you must
assign the count using =.  But translate only counts single character
replacements, not patterns.  So each translation of '../' would return 3.  

Use the substitute operator with the global option instead.  But first you'll have
to move your string into $_.  So your code above should look like this:

if ($_ =~ /<IMG usemap="#map1" src="(.*)titlebar\.GIF" width="467" height="60"
border="0" align="top">/) {
    $_ = $1;
    $back = s/\.\.\//\.\.\//g;
    break;
  }
}

I also added a break if the match succeeds.  No sense reading the rest of the file
if you've found what you're looking for.

That should be enough to get you started.

Don



# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org