LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2005, 10:49 AM   #1
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 366
Blog Entries: 1

Rep: Reputation: 44
Perl: Where am I in a foreach loop?


Is there a way that I can make a reference to the number of the array member(if foreach loop is on $array[1] - the number 1 is what I'm looking for) that I am at in a foreach loop? For instance how can I say:

foreach (@array)
{
if (NUMBER_OF_ARRAY_MEMBER_I'M_ON_NOW>2)
{
do some stuff;
}
}


Yeah, I know it's an ugly example , but I don't know any terminology that would enable me to word it more clearly. And yes, I also realize I could use a regular for loop to do such a thing quite easily, but I am just curious....

Thanks,
jrtayloriv
 
Old 01-30-2005, 11:01 AM   #2
mas00d
LQ Newbie
 
Registered: Jan 2005
Location: India
Distribution: Redhat9
Posts: 13

Rep: Reputation: 0
hi,
As i m new to perl i dont know the best way but i would prefer doing this:

$i=0;
foreach (@array)
{
if ($i > 2)
{
do some stuff;
}
$i=$i +1;
}

Last edited by mas00d; 01-30-2005 at 11:02 AM.
 
Old 01-30-2005, 11:06 AM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
What about this:
Code:
#!/usr/bin/perl

$arr[0]="zero";
$arr[1]="one";
$arr[2]="two";
$arr[3]="three";
$arr[4]="four";
$arr[5]="five";

for($num=0;$num<@arr;$num++){
 if($num>2){print "*"}
 else{print " "}
 print " $num $arr[$num]\n";
}

exit;
 
Old 01-30-2005, 10:43 PM   #4
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 366

Original Poster
Blog Entries: 1

Rep: Reputation: 44
Not exactly what I was looking for - my fault for wording the question poorly. I found the answer in the Camel:

Quote:
There is no way with foreach to tell where you are in a loop. You may compare adjacent elements, by remembering the previous one in a variable, but sometimes you just have to break down and write a three-part for loop with subscripts. That's what the other kind of for loop is there for, after all.
---Section 4.4.3 [Programming Perl]


jrtayloriv
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to break if loop in Perl? Barca Programming 9 08-03-2011 01:15 PM
loop through string with perl lfur Programming 2 07-03-2004 08:05 AM
Need help with perl loop! morbid_ru Programming 1 02-24-2004 01:14 PM
Need help with perl loop! morbid_ru Programming 2 02-17-2004 05:15 AM
perl:foreach katana Programming 3 07-24-2001 01:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration