in reply to Re: hash keys, %
in thread hash keys, %

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

Replies are listed 'Best First'.
Re: Re: Re: hash keys, %
by blokhead (Monsignor) on Jul 29, 2003 at 01:30 UTC
    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