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

Re: [MacPerl] regex help



On 11/27/99 at 3:53 PM, Ronald J Kimball wrote:

: On Sat, Nov 27, 1999 at 11:06:01AM -0800, Don Arbow wrote:
: > On 11/27/99 at 1:31 AM, Nicholas G. Thornton wrote:
: 
: > :                          $back = $bug =~ tr/\.\.\//\.\.\//;
: 
: >  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.
: 
: This is almostly entirely incorrect.
: 
: The =~ operator allows you to use tr/// (and m// and s///) on any scalar
: value, not just $_.  However, use of =~ does _not_ affect tr///'s return
: value.
: 
: $back = $bug =~ tr/\.\.\//\.\.\//; does not assign the modified string to
: $back.  The expression modifies the value of $bug _in-place_, and assigns
: the return value of tr/// to $back.
: 
: The return value of tr/\.\.\//\.\.\// here is not 3.  The target string in
: the example is "../images/images.", which contains three periods and two
: forward slashes, so the return value is 5.
: 
: 
: > 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:
: 
: You don't have to assign the string to $_, but you can if you want to.  If
: you want to user another variable, the =~ operator allows you to do so.
: 
: 
: > if ($_ =~ /<IMG usemap="#map1" src="(.*)titlebar\.GIF" width="467" height="60"
: > border="0" align="top">/) {
: >     $_ = $1;
: >     $back = s/\.\.\//\.\.\//g;
: 
: It would be more correct to only count occurences of ../ at the beginning
: of the string.
: 
: if ($_ =~ /<IMG\ usemap="#map1"\ src="(.*)titlebar\.GIF"\ width="467"
:            \ height="60"\ border="0"\ align="top">/x) {
:    my $path = $1;
:    $count++ while $path =~ m,\G\.\./,g;
: 
: 
: 
: Ronald
: 

Oops, I hadn't finished my morning coffee, so I was a bit off.

In my test case, I used ($back = $bug) which assigned $bug's value to $back. 
Without the parentheses, it does return a valid count to $back from tr///.  I did
mention a count of 3 because in my test case, I had no extra '/'s.

Don



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