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

Re: [MacPerl-AnyPerl] Multiple Substitution



Mark Yannuzzi wrote:

>I want to transform and print to a file the following form of text, which
>are stored in an array:
>
>$label[$i] = 'VD(mA):'
>
>I want to transform them to the following form:
>
>$label[$i] = 'VD [mA]'

Excerpt of message (sent 6 May 1999) by Bart Lateur:
> Mark Yannuzzi wrote:
> Why not take a different, far more flexible angle?
> 
> 	for ($label[$i]) {   # $_ is alias
> 		/(\w+)\((\w+)/ and $_ = "$1 [$2]";
> 	}
		      ^^
unmatched parenthesis ||

Any text before or after the match will be discarded.  Depending on
the application, this may or may not be a problem.  But I like the
use of aliasing.  The way Mark presented the problem looks awfully like it
occurs in a C-like for loop over $i.  (He didn't tell us what he had
in mind with $i), so I'm guessing this:

    for ($i = 0; $i < @label; $i++) {
      for ($label[$i]) {   # $_ is alias
              /(\w+)\((\w+)\)/ and $_ = "$1 [$2]";
      }
    }


Of course that could be simplified to

    for (@label) {
      s/\((.+?)\)/ [$1]/;
      print;				# you said you wanted to print
    }

Yes, nongreedy /.+?/ could be substituted by /[^)\n]+/ here.


	Christian Brechbühler, Dr. sc. techn.
	
	Communication Technology Laboratory, Image Science Group
	Swiss Federal Institute of Technology (ETH), Zurich

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