in reply to hash keys, %

I'm not quite sure what you mean by having a unique URL for each group of twenty. Do you want to generate URLs to pass twenty things at a time to some script?
my @keys = sort keys %hash; my $group = 0; while (my @group = splice(@keys, 0, 20)) { ## generate url from stuff in @group $group++; ## maybe something like this if the items ## are all safe (\w characters): my $url = 'script.cgi?' . join '&', map { "x=$_" } @group; print qq(<a href="$url">group # $group</a><br>\n); }
Even if this isn't quite what you had in mind, maybe the while( .. splice .. ) loop will be a good starting place. The loop condition won't fail until @group gets 0 items, and will still iterate even if @group gets less than 20 the last time through the loop.

Update: if the links only depend on the number of keys, and not the keys themselves, then the number of URLs to print is simply ceil( (keys %hash) / 20 ). ceil simply rounds up, you can import it from the POSIX module.

blokhead

Replies are listed 'Best First'.
Re: Re: hash keys, %
by sulfericacid (Deacon) on Jul 29, 2003 at 01:02 UTC
    I'd like to thank you two for (blokhead and cleverett) for helping with this tricky problem. Of the two methods posted, I went with blockhead's because it looks a lot easier to understand (even though I won't understand fully what's going on until I take a look at splice in a few minutes).

    The code I used is

    my @keys = sort keys %upload; my $group = 0; while (my @group = splice(@keys, 0, 20)) { $group++; my $url = "http://sulfericacid.perlmonk.org/gallery/galleryprint2.pl"; print qq(<a href="$url?page=$group">$group</a><br>\n); }
    What exactly is being stored in @keys? The total of keys from the hash or all the key/value information from the hash and using this as an easier method to count each of them?

    Thanks again for all your help! If you want to see why I needed this, http://sulfericacid.perlmonk.org/gallery/galleryprint2.pl.

    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

      splice(@keys, 0, 20) removes the first 20 items from the array @keys, but almost as important, it also returns the items it removed. So it's like using shift on a larger chunk. You can use splice to simulate push, pop, shift, and unshift, as well as insert and remove items from the middle of an array. Of course, the man page for splice goes over all this in detail, but it's often useful to think of a splice operation in terms of those 4 basic list operators.

      But I would suggest not even using this loop, since you never actually seem to use the stuff in @group that you splice off. You only seem to be interested in the number to print out, so you could just as easily do this:

      use POSIX 'ceil'; my $url = "http://..."; my $num_pages = ceil( (keys %hash) / 20 ); print qq(<a href="$url?page=$_">$_</a><br>\n) for 1 .. $num_pages;
      Even if you do use this method, you should still get familiar with splice!

      blokhead