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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For loop?

Status
Not open for further replies.

jae21

Technical User
Jul 13, 2001
6
US
I was wondering how to do an enumerated for loop? I wanted to go through the elements of an array by number but only know of the foreach loop.
 
Actually, for and foreach are synonyms. Use can use either word. The difference is in the rest of the statement. You can do (assuming an array named @Values):
Code:
for my $val (@Values) {print "$val\n";}
# or #
for ( my $x=0; x<=$#Values; x++ ) {print &quot;$Values[$x]\n&quot;;}
# or even #
for my $x (0..$#Values) {print &quot;$Values[$x]\n&quot;;}
Hope that helps.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks tracey, that helped a lot.

I've been able to implement an incrementing for loop even though i don't think i really need to :) But for some reason I get an error when running this bit of code.

for my $i (0..$#records)
{
chomp($records[$i]);
($rectitle, $reckeywords, $recurl) = split(/\|/, $records[$i]);
@tempurl = split(/\#/, $recurl);
if $url[0] eq $tempurl[0]
{
if $url[1] lt $tempurl[1]
{
$tempurl[1]++;
}
if $url[1] eq $tempurl[1]
{
$tempurl[1]++;
print OUTDB &quot;$videotitle \| $videokeywords \|$videourl\n&quot;;
}
$recurl = join(&quot;#&quot;, @tempurl);
}
print OUTDB &quot;$rectitle \| $reckeywords \|$recurl\n&quot;;
}
 
I believe that you are required to have parens around your conditions on your if statements, like this:
Code:
if ($url[0] eq $tempurl[0])
That's the only thing I can see might be wrong.

Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
tracy, thanks so much for helping out a clueless newbie. i think i'm pretty much done the little project that i was working on so i won't bother you any more :)
 
You're very welcome for the help. It's no bother at all! That's what we're here for. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top