sulfericacid is right, there's no reason to use negative indices to do this; you start counting page numbers from the beginning of the array anyway, and it's not too tough to calculate the start and end indices of the range. Here's some pagination code I use in my photo album software, which correctly handles all edge cases I could think of at the time I wrote it:
use POSIX 'ceil'; my @things = 1 .. 42; my $per_page = 20; my $num_pages = ceil( @things / $per_page ); my $page = param('page') || 1; ## verify page num is within range $page = 1 if $page < 1; $page = $num_pages if $page > $num_pages; my $first = ($page-1) * $per_page; my $last = $first + $per_page - 1; $last = $#things if $last > $#things; print @things[ $first .. $last ];
I also at least 2 odd things about this line:
for ( grep defined($_), ( reverse keys %upload )[ $top .. $bottom ] ) +{
1: You're using the keys of a hash to paginate. This is a very bad idea. When you add something to the hash, the ordering of keys %upload can change. To make things worse, in the newest (and all future) versions of Perl, the ordering of keys will completely change between program instances, even if the data hasn't changed. This would make your pagination useless, as you would basically get a random page of 20 things each time, no matter what page number you give to your CGI script. Use an array of things, or else sort the hash keys to make the ordering consistent between datasets and program instances.

2: Why do you have grep defined($_) ?? Are you expecting an undef hash key? Did you really mean grep exists $upload{$_} instead?

HTH

blokhead


In reply to Re: math mistake? by blokhead
in thread math mistake? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.