Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Finding length of line if have any position of any char inside line

by daxim (Curate)
on Oct 10, 2019 at 11:40 UTC ( #11107301=note: print w/replies, xml ) Need Help??


in reply to Finding length of line if have any position of any char inside line

You don't need to build an array.
use experimental 'signatures'; sub length_at_position_between($s, $p, $n = "\n") { my ($prev, $next); while ($s =~ /$n/g) { $prev = $next; $next = pos $s; last if $next > $p; } return $next - $prev; } print length_at_position_between ("test\nI want length of this line\n test", 12); # 27
  • Comment on Re: Finding length of line if have any position of any char inside line
  • Download Code

Replies are listed 'Best First'.
Re^2: Finding length of line if have any position of any char inside line
by rjt (Curate) on Oct 10, 2019 at 12:50 UTC

    As with the OP's version that you based yours on, this fails several of these tests:

    my @tests = ( # $string, $pos, $expected, $description [ "\nString\n", 2, 6, 'Surrounded' ], [ "String\n", 2, 6, 'No first \n' ], [ "\nString", 2, 6, 'No last \n' ], [ "String", 2, 6, 'No \n' ], [ "", 0, 0, 'Blank' ], );

    Since the OP's version failed those as well, this is understandable. Obviously even more tests would be needed, but this was meant as more of a sanity check than anything.

    Performance-wise, here's where your solution fits in:

    Rate op_ver daxim rlindex cur_strlen op_ver 564186/s -- -23% -66% -86% daxim 737063/s 31% -- -56% -81% <-- H +ERE rlindex 1677856/s 197% 128% -- -57% cur_strlen 3932672/s 597% 434% 134% --

    Of course, I am not sure what sort of performance impact (for better or worse) a fixed version of your code would have. Complete code for my test and benchmark suite is in my comment, if you want to have a go with it yourself.

    not ok 1 - daxim: Surrounded # Failed test 'daxim: Surrounded' # at /home/ryan/src/perlmonks/subline.pl line 37. # got: '7' # expected: '6' Use of uninitialized value $prev in subtraction (-) at /home/ryan/src/ +perlmonks/subline.pl line 64. not ok 2 - daxim: No first \n # Failed test 'daxim: No first \n' # at /home/ryan/src/perlmonks/subline.pl line 37. # got: '7' # expected: '6' Use of uninitialized value $prev in subtraction (-) at /home/ryan/src/ +perlmonks/subline.pl line 64. not ok 3 - daxim: No last \n # Failed test 'daxim: No last \n' # at /home/ryan/src/perlmonks/subline.pl line 37. # got: '1' # expected: '6' Use of uninitialized value $prev in subtraction (-) at /home/ryan/src/ +perlmonks/subline.pl line 64. Use of uninitialized value $next in subtraction (-) at /home/ryan/src/ +perlmonks/subline.pl line 64. not ok 4 - daxim: No \n # Failed test 'daxim: No \n' # at /home/ryan/src/perlmonks/subline.pl line 37. # got: '0' # expected: '6' Use of uninitialized value $prev in subtraction (-) at /home/ryan/src/ +perlmonks/subline.pl line 64. Use of uninitialized value $next in subtraction (-) at /home/ryan/src/ +perlmonks/subline.pl line 64. ok 5 - daxim: Blank
    use strict; use warnings; omitted for brevity.

      Thanks. I have handled edge cases. For simplicity edge cases omited in my example i want to focus on main problem first

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11107301]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2020-09-20 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (120 votes). Check out past polls.

    Notices?