use strict; my %idx; # create index... while () { last if /# Prints/; # for this demo $idx{$1} = (tell(DATA) - length) if /^\s*(\S+)/; } # E.g., access each line in "natural" hash order (more/less random)... print "offset: \n"; foreach (keys %idx) { seek(DATA, $idx{$_}, 0); chomp (my $line = ); printf("%6d: %-7s <%s>\n", $idx{$_}, "<$_>", $line); } __DATA__ data1 122 1223 12223 12223 data2 12122 12223 122223 122223 data3 13422 134223 4512223 982223 data4 23432 3432 234234 789879 data5 5635 9786 23423 2323423 # Prints offset: 486: < data4 23432 3432 234234 789879> 418: < data2 12122 12223 122223 122223> 451: < data3 13422 134223 4512223 982223> 518: < data5 5635 9786 23423 2323423> 390: < data1 122 1223 12223 12223>