Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test for last element in foreach loop 1

Status
Not open for further replies.

nawlej

Programmer
Mar 26, 2004
380
US
if I have a foreach loop testing each element in an array, with label "ASTAT", how would I tell the script to do something unless hte script has reached the last element in the array.
 
Code:
for(my $x = 0; $x < $#array;$x++){
   # do something to all but last element in the array...
}

Sometimes it is easier to do w/o the beautiful foreach style loops.

--jim
 
Code:
my $count=0;
foreach (@array) {
  if ($count != $#array) {# $#array is the number of elements in the array
    #do something with $_
  }
  $count++;
}

Haven't tested this, there might be a need to move the $count++ line

HTH
--Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top