Hi Jim,
Well, instead of asking at each step, whether the last element is processed
or not, it's slightly better to exclude it from the loop:
for my $i (0..$#keyFields - 1) {
printf "%s %s", $keyFields[$i], $match;
}
printf "%s %s", $keyFields[-1], $lastmatch;
But yes, I guess your last approach is just what the doctor ordered in this
case. ) I'd use a map, though:
print join ' AND ', map { "$_ GE 0" } @keyFields;
.. as, again, it'd be slightly more efficient to allocate the space for the
resulting array just once.
-- iD
2012/2/15 Jim Gibson <[email protected]>
> At 9:08 PM -0700 2/14/12, Chris Stinemetz wrote:
>
>> I have a for loop I would like to alter so that when the iteration
>> reaches the last element in the array the varibale $lastmatch is
>> passed to printf instead of $match.
>>
>> Does anyone have any suggestions?
>>
>> my $match = "GE 0 AND ";
>> my $lastmatch = "GE 0";
>>
>> for my $i (0 .. $#keyFields) {
>> printf "%s %s", $keyFields[$i], $match,;
>> }
>>
>
> There are several approaches:
>
> 1.
>
>
> for my $i (0 .. $#keyFields) {
> printf "%s %s", $keyFields[$i], ($i<$#keyFields?$match:$**lastmatch);
> }
>
> 2.
>
>
> for my $i (0 .. $#keyFields) {
> if( $i == $#keyFields ) {
> printf "%s %s", $keyFields[$i], $lastmatch;
> }else{
> printf "%s %s", $keyFields[$i], $match;
> }
> }
>
> 3.
>
> my @output;
> for my $field (@keyFields) {
> push( @output, sprintf("%s GE O ",$field);
> }
> print join("AND",@output);
>
>
>
> --
> Jim Gibson
> [email protected]
>
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
>