Kathryn Bushley am Samstag, 21. Oktober 2006 21:43:
> Hello again,
>
> Thanks Tom Pheonix...I had put in a forward in place of backwards
> slash...always a stupid mistake...one more question...I am able to
> substitute the first value in my hash %id_global but doesn't substitute the
> rest I think because it is only moving through the file once...I've
> successfully split it into an array...how does one go about whiling through
> the elements of an array (@TREE)?
>
> open (TREE,$treefile)|| die "can't open tree file: $\n";

typo: '$' instead of '$!'

> my $line;

Can be declared within the while loop.

> while (<TREE>){
>    foreach my $code (keys %id_global){
>      $line = $_;

This assignement should be placed outside the foreach loop
(see comment below)

>      $line=~s/(.*)$code([\D])/$1$id_global{$code}$2/g;
>    }
> }

The locgical problem here is that after substitution in the inner foreach 
loop, you discard $line and assign the original value from $_ for every 
subsequent substitution. 
Move the line "$line = $_;" above the foreach loop to avoid this.

Also, It *might* be an idea to replace "(.*)" [greedy] in the regex with 
"(.*?)" [non-greedy], because it would try to replace multiple occurances
of a $code from first to last and not from last to first. 
Hm... I think you can even omit the $1 part and just write:

  $line=~s/$code([\D])/$id_global{$code}$1/g;

Btw: You follow the strategy: "Search for all existent codes (in %id_global)
and replace if present (in $line)". If there are much more entries in 
%id_global than in a $line and the format of the codes can be specified 
as regex, it might be more performant to change it to 
"Search present codes (in $line) and try to replace them".


hth,

dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to