For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > whiling through an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author whiling through an array
Kathryn Bushley

2006-10-21, 6:57 pm

Hello again,=0A=0AThanks Tom Pheonix...I had put in a forward in place of b=
ackwards 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 th=
e rest I think because it is only moving through the file once...I've succe=
ssfully split it into an array...how does one go about whiling through the =
elements of an array (@TREE)? =0A=0Acheers,=0A=0Akathryn=0A=0A=0A#open
treef=
ile and while through it, substituting species name value when key found in=
file.=0Aopen (TREE,$treefile)|| die "can't open tree file: $\n";=0A#my @TR=
EE;=0Amy $line;=0Awhile (<TREE> ){=0A #split $_ on colon;=0A #@TREE =3D =
split (':',$_);=0A #print $TREE[0]; =0A foreach my $code (keys %id_glob=
al){=0A #print "CODE->".$code."<-VALUE->".$id_global{$code}."\n";=0A $l=
ine =3D $_;=0A #if ($line =3D~ m/(.*)$code([\D])/) {print "MATCH"." ".$co=
de."VALUE->".$id_global{$code}."<-\n";} =0A #else {print "NO =
MATCH"." ".$code."<-\n";} #works,matches all values w/right val=0A #$line=
=3D~ s/(.*)$code([\D])/$1$id_global{$code}$2/g; #Yea!...now it substitutes =
one value correctly=0A print $line.":";=0A }=0A}=0A=0A=0A
D. Bolliger

2006-10-22, 3:57 am

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com