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

PERL: Should I use count, or specify which parts of an array to print?

Status
Not open for further replies.
Feb 14, 2002
88
JP
Okay -- quick summary:

I've got a lot of thumbnails in a directory, and I want to display 15 on each page, and have a next button to grab the next 15.

Here's what I've got to feed the dirlist of thumbnails into the array:

opendir DIR, ".";
@thumbs = grep /1s.jpg/i, readdir(DIR);
foreach $_ (@thumbs) {
print "$_\n";
}

So, the above will print them all out (I'm only working w/ a shell right now -- not w/ HTML, but I'll worry about that later.

I tried using something like
opendir DIR, ".";
@thumbs = grep /1s.jpg/i, readdir(DIR);
$count = 0
foreach $_ (@thumbs) {
while ($count < 15) {
print &quot;$_\n&quot;;
$count += 1;
}
}
but for some reason, that just printed the first one 15 times, so I'm thinking I should print it out according to it's order in the array.

However, after thinking about it, that doesn't seem too dynamic. I want to eventually just throw thumbnails in the folder, and not have to adjust the script.

On that note, would the 'next' button I'm wanting actually hold a variable and reload the script? I'm not quite sure how it would know to load the next 15 instead of the first 15 again.

I'm looking for ideas here more than code I think, although some of the code wouldn't hurt. :)

Thanks.
 
You could try using a for loop instead of a foreach loop. By setting the counter to the variables passed to the script, you could set the test to see if it's below 15 and below the number of elements in the array. It would look something like this:
for ($i = param(&quot;start&quot;); $i < 15 && $i < scalar(@array); $i++)
....
I think this would do what you want. I am a bit uncertain with the syntax since I haven't used PERL's for loop in a long time. //Daniel
 
Okay, I got this to work:

opendir DIR, &quot;.&quot;;
@thumbs = grep /1s.jpg/i, readdir(DIR);
for ($count=0; $count < 15; $count++){
print &quot;@thumbs[$count]\n&quot;;
}
closedir DIR;

That does what I want it to do for that page perfectly. However, I'm still stumped on how the 2nd page works. I would assume the easiest way is to duplicate the script, and change the loop to be:
for ($count=15; $count < 30; $count++){
and have the 'next' button point to this script, and then so on and so forth, but I tend to think I'm overlooking something, and that there's a way to do it all within one script.

Kind of wish Perl was able to use the URL as ASP does. :)
 
Perl is able to use a query string. The CGI module was written for this purpose. Your script would look something like this:
#!/path/to/perl
use CGI;
$cgi = new CGI;
#other code here...
opendir DIR, &quot;.&quot;;
@thumbs = grep /1s.jpg/i, readdir(DIR);
if ($cgi->param(&quot;start&quot;) !~ /^[0-9]+$/ || $cgi->param(&quot;start&quot;) > scalar(@thumbs))
{
$count = 0;
}
else
{
$count = $cgi->param(&quot;start&quot;);
}
$end = $count + 15;
for (; $count < $end; $count++){
print &quot;@thumbs[$count]\n&quot;;
}
closedir DIR;
Then the next page would be scriptname?start=15 and so on. //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top