User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 374,147 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,492 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser:
Views: 334 | Replies: 2
Reply
Join Date: Nov 2007
Posts: 4
Reputation: SNN is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SNN SNN is offline Offline
Newbie Poster

if the index is an even number

  #1  
May 14th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: if the index is an even number

  #2  
May 14th, 2008
  1. for($j=0; $j<=100; $j++){
  2. <do the expression> unless($j%2);
  3. }
If you are doing some mathematical operation with the expression i think you want to re-check whether loop should start from 0.

katharnakh.
challenge the limits
Reply With Quote  
Join Date: Mar 2006
Posts: 560
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 30
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Posting Pro

Re: if the index is an even number

  #3  
May 15th, 2008
If you want to process the even number indices of an array:

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

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Perl Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 3:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC