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 "