Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have an array holding codes like ABC, DEF, GHT etc. I want to look through a text file and replace any matches to the array codes with the position that code holds in the array.

ie. so if my codes are $code[0]=ABC and $code[1]=DEF and the file contains the text ABC and DEF it gets replaced with the numbers 0 and 1 instead.

I have tried this:

while(<DATA>) { for($a=0;$a scalar@codes;$a++) { s/$codes[$a]/$a/; } } print $_;
Which doesn't work. I'm probably trying something really dumb, but I hoped the substitution might be able to work like that. Any ideas on better methods ?

Thanks a lot,

anon

Replies are listed 'Best First'.
Re: Match text in file with array and substitute array position ?
by chromatic (Archbishop) on Nov 29, 2002 at 19:14 UTC
    my %codes = ( ABC => 0, DEF => 1, GHT => 2 ); my $alternation = join('|', keys %codes); my $substitution = qr/($alternation)/; while (<DATA>) { s/$substitution/$codes{ $1 }/g; print; }

    Untested, but it can't be too far wrong.

Re: Match text in file with array and substitute array position ?
by arturo (Vicar) on Nov 29, 2002 at 17:42 UTC

    Here's how your for line reads, assuming you have 9 things in your array:

    for ( $a=0;$a 9; $a++)

    I'll leave it up to you to figure out what the missing character is =)

    Also, note that your substitution will only replace the first occurrence of each code in each line. You'll need to add the /g modifier if you want to change all the matches.

    Also, note that $a is a special variable in Perl, it's a package global used by sort subroutines and it's not usually the best choice for a throwaway.

    If not P, what? Q maybe?
    "Sidney Morgenbesser"

Re: Match text in file with array and substitute array position ?
by John M. Dlugosz (Monsignor) on Nov 29, 2002 at 17:44 UTC
    Other than the typo (missing less-than symbol), I think you just need the g modifier on the regex, so it will do more than the first occurance on the line.

    If the array holds things other than letters (e.g. '*'), you will need to escape them out! Other than that, it looks like it should work.

    —John