DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Perl (http://www.daniweb.com/forums/forum112.html)
-   -   if the index is an even number (http://www.daniweb.com/forums/thread124266.html)

SNN May 14th, 2008 7:13 pm
if the index is an even number
 
Hi all ,

I have loop and I would like to calculate an expression only when the index is even

for($j=0; $j<=100; $j++){

if ( ){
do the expression
}
}
i.e how do I tell perl to do the if statement when $j is an even number?

Thanks

katharnakh May 14th, 2008 11:18 pm
Re: if the index is an even number
 
for($j=0; $j<=100; $j++){ 
<do the expression> unless($j%2);
}
If you are doing some mathematical operation with the expression i think you want to re-check whether loop should start from 0.

katharnakh.

KevinADC May 15th, 2008 2:52 am
Re: if the index is an even number
 
If you want to process the even number indices of an array:

for (my $i = 0; $i <= $#array; $i+=2) {


All times are GMT -4. The time now is 4:50 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC