my @strings = split "\n", $string; my @lookup; $lookup[0] = 0; foreach my $i ( 1..$#strings ) { # byte offset of 1st char on this line $lookup[$i] = length($strings[$i-1]) + $lookup[$i-1]; } #### #!/your/perl/here use strict; use warnings; my $string = < $offset ) { $hi = $line; } elsif ( $lookup[$line] < $offset ) { $lo = $line; } else { # small chance it's equal last; } } # could have overshot 1 if ( $lookup[$line] > $offset ) { $line--; } if ( $lookup[$line+1] > $offset ) { # this is the one my $line_offset = ($offset - $lookup[$line]); return ($line, $line_offset); } else { return; } }