PDA

View Full Version : How to get next value in a foreach?


chrisvmarle
06-29-2004, 12:33 PM
I have a piece of code to create html-files of pictures in a dir. I would like to create a link to the next picture on each page.

The problem is, I don't know how to get the name of the next picture in @list.

Here's a piece of my code:
# code removed here
print "open dir $dir\n";
opendir(DIR, "$dir");
print "reading dir\n";
my @list = sort(grep(/.jpg$/i, readdir(DIR)));
closedir(DIR);

# code removed here

foreach my $item (@list)
{
(my $basename, my $extension) = split(/\./, $item);

# lots of code removed here

$nextlink = ""; # I need the value of the next $item here

print "creating html file\n";
open(FILE,">$dir/$basename.html") || die("Couldn't create new html file ($dir$
print FILE qq~<html><head><title>$basename</title></head>
<body>
<center>
<img src="$fulls/$item" width="$newfw" height="$newfh" border="0"><br>
$prevlink$toplink$nextlink
</center>
</body></html>~;
close(FILE);

$prevlink = qq~<a href="$basename.html">prev</a> ~;
}


Any suggestions how to fill $nextlink with the right link?

Thanks in advance

MattJakel
06-30-2004, 08:36 AM
I would do this:


#Cutting off code before foreach statement and adding one line before it

my $nextnum = 1;
foreach my $item (@list)
{
(my $basename, my $extension) = split(/\./, $item);

# lots of code removed here

$nextlink = "<a href=\"@list[$nextnum]\">Next</a>"; #I didn't see the <a> tag anywhere else so I slipped it in there.
$nextnum++; # This line added

#And more code cut off


Hope this helps,
Matt

chrisvmarle
06-30-2004, 08:51 AM
Thanks!

How could I not think of that? :S

Guess it's been to long since I've been working with Perl...