Library ( 0 -> [all the data from the file which starts with a value between 1 and n] 1 -> [all the data from the file which starts with a value between n and 2n] .... 7 -> [all the data from the file which starts with a value between 7n and 8n] ); #### #!/usr/local/bin/perl -w use strict; use Data::Dumper; my @column_info = ([ 1024, 1067 ], # Column 1 start stop [ 1068, 1093 ], # Column 2 start stop [ 1094, 1137 ], # Column 3 start stop [ 1138, 1163 ], # Column 4 start stop [ 1164, 1207 ], # Column 5 start stop [ 1208, 1233 ], # Column 6 start stop [ 1234, 1277 ], # Column 7 start stop [ 1278, 1321 ] ); # Column 8 start stop # Note that we don't have to cat this to read the data. # We can just open for reading, hence the "<". # Note also that we check the return value for open. # This is very very important. open(IN, "< /tmp/show_library.$$") or die "Failed to open /tmp/show_library.$$ for reading: $!"; my @Library; LINE: while() { s/^ //; # works on $_ by default. chomp; # split on spaces and take the first value my $column = (split(' ', $_))[0]; for(my $i=0; $i < @column_info; $i++) { my ($start, $stop) = @{$column_info[$i]}; # if this is the right range, put it in our library if($column >= $start && $column <= $stop) { push @{$Library[$i]}, $_; next LINE; } } # If we've got to here we haven't found the range for # this entry. We can do whatever we like, but here I # push it on to the very last element of Library, after # the final range value push @{$Library[@Library]}, $_; } # To verify we have the data structure we wanted: print STDERR Dumper(@Library); # Note that the entries are not sorted by their inserted value. # That isn't too hard to resolve. foreach my $entry (@Library) { @$entry = sort {(split(' ', $a))[0] <=> (split(' ',$b))[0]} @$entry; } # To verify we have the ordering we wanted: print STDERR Dumper(@Library);