my $search = sprintf("%03d",20); # pad the search thingy my $ind_length = 3; # length of sorted index column my $line_length = 8; # line length my $size = (-s $file); die "Corrupt file" unless $size % $line_length == 0; open(IND,$file) || die $!; # get the file ready my $min = 0; # lower bound my $max = $size; # upper bound my $result = undef; my $match; while(1){ my $pos = $ind_length * int( ($min/$ind_length + $max/$ind_length) / 2 ); # find the middle (make sure it is at the # beginning of a row last if $pos == $min; # there is no result seek(IND,$pos,0); # go to the middle sysread(IND,$match,$ind_length); # read the info # found it! if( $match eq $search ){ seek(IND,($pos+$ind_length+1),0); sysread(IND,$result,$ind_length); # read the real index last; } if( $match gt $search ){ $max = $pos; }else{ $min = $pos; } } if( defined $result ){ print "Found it ($result)\n"; }