[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]";
>       }
>     }

Sorry, I was unspecific, and possibly (unintentially) misleading.  I have
data files that are input to my script, which contain lines (among many
others) that look as follows:

!CHIP(y,x): 056,024 VD(V): 5 ID(mA): 257.42 VG(V): -.279
                    ^^^^^^   ^^^^^^^        ^^^^^^
                    label    label          label

that need to be parsed.  Namely, I need the indicated substrings as column
labels for an output file that my script generates.  These end up as
elements of an array, which I tried to indicate with the statement:

$label[$i] = 'VD(mA):'

I ended up using:

    $dieParams[0][$j] =~ s/\((\w+)\):/ [$1]/;
    print OUTFILE ($dieParams[0][$j], "\t");

after all the helpful (almost instantaneous) input from fellow members of
this mailing list (THANKS ALL!).  The crux of my problem was that I fogot
about my single encounter with backreferences, which is the tool I was
looking for.  Additionally, I learned about the 'map' function (thanks
Carl), which would allow me to combine the two statements into one, but it
is easier for me to read the two as they are now.

Mark
----------------------
Mark J. Yannuzzi
myannuzzi@aya.yale.edu

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