in reply to Find the boundaries of a substring in a string

TIMTOWTDI. Using your code as a starting point I would use pos to find the end point of the sequence. Here is a runnable example.

use strict; use warnings; my $seq = 'dddddddddBBBBBBBBBBBBDDDDDDDDBBBBBBBBBBBBBBddddddddddddddddddddBBBB +BBBBBBBBBBDDBBBBBBBBddddddddddddd'; while ($seq =~ /(B+)/g) { my $seg = $1; my $seg_length = length ($seg); my $seg_end = pos ($seq); my $seg_start = $seg_end - $seg_length; print $seg. "|" . $seg_start . "-" . $seg_end . "\n"; }

🦛

Replies are listed 'Best First'.
Re^2: Find the boundaries of a substring in a string
by Anonymous Monk on Jun 27, 2023 at 09:24 UTC
    Thank you both! I noticed in both snippets that the end boundaries are off by 1, or am I counting wrong?

      Are you counting from 1 or zero? If from 1 then it should probably be this (to give the first match at 10 to 21 inclusive):

      use strict; use warnings; my $seq = 'dddddddddBBBBBBBBBBBBDDDDDDDDBBBBBBBBBBBBBBddddddddddddddddddddBBBB +BBBBBBBBBBDDBBBBBBBBddddddddddddd'; while ($seq =~ /(B+)/g) { my $seg = $1; my $seg_length = length ($seg); my $seg_end = pos ($seq); my $seg_start = $seg_end - $seg_length + 1; print $seg. "|" . $seg_start . "-" . $seg_end . "\n"; }

      To say what you expect the answer to be when posting have a read of How to ask better questions using Test::More and sample data.


      🦛

        Counting from 0, as usual. If you check the first substring, then it starts (correctly) at 9, but it should end at 20, not 21, am I wrong? It has a length of 12, and it includes position 9.