Step 1: Find some way to get a list of hash keys that has a stable order. Hashes are unordered so theres no way to know where any 6 items happen to lie in correlation to each other. My first thought would be just sorting the keys but other methods will work.
Step 2: Create a param that stores what page you're on and pass it in your "next" link. Example, foobar.com?page=3. Page is your "counter variable".
Step 3: Multiply your counter variable by the number of items you want to display per page as an indice in to your array.
Step 4: Use your indices to extract a list of keys from your array of keys, then use this list to extract a list of elements from your hash.
Sample code:
use CGI;
my $c = CGI->new();
my %hash = qw/data and stuff/; #however you fill the hash
#Step 1
my @keys = sort keys %hash; # this should maintain the same order, ass
+uming hash doesn't change
#Step 2
my $counter = $c->param('page');
#step 3
my $begin = $counter * $items_per_page;
my $end = $begin + $items_per_page;
#step 4
print @hash{ @keys[ $begin..$end ] }; #deep voodoo
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.