in reply to scale of large data file and output them in certain format

Not sure if this can help you...
#!/usr/bin/perl use strict; my $file_1 = <<EOF; 0001Hadrian 0002Claudius 0003Julian 0004Augustus 0005Severus 0006Constantine 0007Valentinian EOF my (%record, $count_line); my $page_number = 1; open( my $fh, "<", \$file_1 ) or die "Could not open file.\n"; while (<$fh>) { chomp; s/(\d{4})//; $record{$1}->{$_}->{'Master'}++; } close( $fh ); while (<DATA>) { chomp; s/(\d{4})//; $record{$1}->{$_}->{'Child'}++; } for my $i ( sort keys %record ) { for my $j ( sort keys %{ $record{$i} } ) { if ( defined( $record{$i}->{$j}->{'Master'} ) ) { print "\t\t\t$j\n"; } else { print "$j\n"; } $count_line++; if ( $count_line == 5 ) { print "\n\t\tPage is $page_number\n\n"; $page_number++; $count_line = 0; } } } __DATA__ 0001Legio IX 0001Legio VI 0001Ursa Minor 0002Veraculum 0002Thrace 0002Legio III 0003Persia 0003Londinium 0004Legio II 0005Caledonia 0005York 0006Christianity 0006Constantinople 0006Legio II 0007Julian 0007Legio VII