my $links_per_page = 100; ### or any number ### get number of pages ### modulo is your friend my $num_pages = int (( scalar @link ) / $links_per_page); $num_pages++ if @link % $links_per_page; my $page = 0; my $counter = 0; my $file; my $other_pages; ### create per-page links for ( my $i = 1; $i <= $num_pages; $i++ ) { $other_pages .= "$i\n"; } foreach ( @link ) { ### again, modulo is your friend here if ( ( $counter % $links_per_page ) == 0 ) { ### we create a new page here $page++; my $prev = $page - 1; my $next = $page + 1; $file = 'page_' . $page . '.html'; open Fh, "> $file" || die "Can't write to $file: $!\n"; ### header info and such print Fh "Page $page"; print Fh "\n"; print Fh "

Page $page of links

\n"; ### we only want a 'previous' link if there's ### a previous page if ( $page > 1 ) { print Fh "
"; print Fh "Previous\n"; } print Fh $other_pages; ### we only want a 'next' link if there are ### more pages after this one if ( $page < $num_pages ) { print Fh ""; print Fh "Next"; } print Fh "
\n"; } print Fh "$_\n"; ### print a link $counter++; } close Fh; ### The other files got closed when we opened ### new files using the same handle.