in reply to Finding length of line if have any position of any char inside line
TMTOWTDI - Why search for things when regex will do that for you? It may not be the fastest, or it may, I don't care, but does that really matter?
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11107298 use warnings; my $string ="test\nI want length of this line\n test"; my $position = 12; pos($string) = $position; $string =~ /.*\G.*\n?/; my $length_of_line = length $&; print "length of line at $position is $length_of_line\n";
Outputs:
length of line at 12 is 27
|
|---|