John W. Krahn wrote: > John W. Krahn wrote: >> >> It looks like this will do what you want:
John, Since you are not using the offset, and you have the same value GG twice and index starts over from the first, you are counting the same GG twice. If you remove the last GG, you will still come up with 4 and it should be 2 unless I am missing something. I believe the individual was wanting to seek down the path and not restart each time, though they can correct us if wrong there. Wags ;) Wags ;) >> >> sub score { >> my ( $str, $array ) = @_; >> my $total_score = 0; >> >> for my $frag ( @$array ) { >> my $len = length $frag; >> my $idx = index $str, $frag; >> >> if ( $idx >= 0 and substr $str, $idx, $len, '' ) { >> $total_score += $len; } >> } >> return $total_score; >> } > > Which can be written more compactly as: > > sub score { > my ( $str, $array ) = @_; > my $total_score = 0; > > for my $frag ( @$array ) { > ( my $idx = index $str, $frag ) >= 0 or next; > $total_score += length substr $str, $idx, length $frag, ''; > } > return $total_score; > } > > > > :-) > > John > -- > use Perl; > program > fulfillment ******************************************************* This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. ******************************************************* -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>