Perl 4.036 - Simple Question ? 
Author Message
 Perl 4.036 - Simple Question ?

#
#
# Hi there,
#
# can somebody explain, why this small script does not really work?
#
# It should replace any occurance of '44' by somthing entered via standard input.
#
# Result is:
# ABC44FGH44: replace 44 with ? a
# IJK44: replace 44 with ? b
# QRS44: replace 44 with ? c
# ABC44: replace 44 with ? d
# ABCdFGHaIJK
# IJK44: replace 44 with ? e
# QRS44: replace 44 with ? f
# IJKeNOP
# QRS44: replace 44 with ? g
# QRS44: replace 44 with ? h
# ABC44FGH44: replace 44 with ? i
# IJK44: replace 44 with ? j
# QRS44: replace 44 with ? k
# ABC44: replace 44 with ? l
# ABC44: replace 44 with ? m
# QRSgVWX
#
# Shouldn't there be only 4 substitutions and therefor 4 replacement prompts?
#

         "IJK44NOP",
         "QRS44VWX");


{
   while ($ele =~ /(\w+)(44)(\w+)/g)
   {  
     print "$1$2: replace 44 with ? ";
     $s = <>;
     chop ($s);
     $ele =~ s/($1)44($3)/$1$s$2/g;
   }
   print "$ele \n";

Quote:
}




Sun, 23 Nov 1997 03:00:00 GMT  
 Perl 4.036 - Simple Question ?
I think the problem has to do with the greediness of regular expressions.  I
assume (perhaps incorrectly) that you would like to replace each occurrence of
"44" with user selected text.  If that is the case perhaps something like the
following will suffice (followed by sample session):


   {
   while($ele =~ /44/g)
      {
      $before = $`;
      $after  = $';
      printf "%-15s: replace 44 with? ", $before . 44;
      chop($s = <STDIN>);
      $ele =~ s/($before)44($after)/$1$s$2/;
      }

   print "$ele\n";
   }

ABC44          : replace 44 with? a
ABCaFGH44      : replace 44 with? b
ABCaFGHbIJK
IJK44          : replace 44 with? c
IJKcNOP
QRS44          : replace 44 with? d
QRSdVWX

Hope this helps.
Jim Shapiro



Sun, 23 Nov 1997 03:00:00 GMT  
 Perl 4.036 - Simple Question ?

Quote:
>...
># Shouldn't there be only 4 substitutions and therefor 4 replacement prompts?
>#


>         "IJK44NOP",
>         "QRS44VWX");


>{
>   while ($ele =~ /(\w+)(44)(\w+)/g)
>   {  
>     print "$1$2: replace 44 with ? ";
>     $s = <>;
>     chop ($s);
>     $ele =~ s/($1)44($3)/$1$s$2/g;
>   }
>   print "$ele \n";
>}

The g-flag (in s/..../..../g ) makes the trouble. Without 'g' it works fine.

BTW: you may want to replace '44' from left to right. The following script works:


{
   while (/44/)
   {  
     print "$`$&: replace 44 with ? ";
     $s = <>;
     chop ($s);
     s/44/$s/;
   }
   print "$_\n";

Quote:
}

All the best,
Gyula Szalay

---------------------------------------------------------------------------

 German National Research Center for Computer Science (GMD)
 Rathausallee 10, D-53754 St. Augustin
 Tel: +49 2241 14 3279, Fax: +49 2241 14 3006



Tue, 25 Nov 1997 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Perl 4.036 port to Win-NT question

2. Perl 4.036 pattern question

3. Question on Formats (Perl 4.036)

4. question about the 4.036 debugger

5. Newbie question re: 4.036 and 5.001m

6. Describe Perl Symbol version 1.8 for perl 4.036

7. PERL 4.036 or Perl 5.x with Unixware 2.1

8. Perl 5.001 vs Perl 4.036...

9. Converting from Perl 4.036 to Perl 5.0

10. XS - Simple answer to simple question?

11. Simple Question from a simple chap!

12. Simple Question - LWP::Simple not accessing the internet

 

 
Powered by phpBB® Forum Software