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

foreach print question for multiple pages

Status
Not open for further replies.

ability1

Programmer
Dec 24, 2002
23
0
0
US
if I have a large array how could I set a counter to display x number of results to page 1 next results to page 2 and so on?

 
Decide how many rows you want to print per page.
As you loop over your array, start a new page any time the
array index plus 1 is evenly divisible by the number of rows you want per page. You need to add 1 to the array index before dividing, since array indexing starts at 0.
Code:
#!perl
use strict;
my @arr = (1..30);
my $rowspp = 10;

for (my $i=0; $i<@arr; $i++) {
    print $arr[$i], "\n";
    if (($i+1) % $rowspp == 0) {
        if ($i+1 < @arr) {
            print "--NEW PAGE--\n";
        }
    }
}

Example output:
1
2
3
4
5
6
7
8
9
10
--NEW PAGE--
11
12
13
14
15
16
17
18
19
20
--NEW PAGE--
21
22
23
24
25
26
27
28
29
30
I added the "if ($i+1 < @arr)" condition to make it not print "NEW PAGE" if we're at the end of the array; if you want it there too, just take that out.
 
would I use this same context if I wanted next page to be hyperlinks ie(page2, 3, 4) of the results?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top