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/


Reply via email to