in reply to Re^3: Find the boundaries of a substring in a string
in thread Find the boundaries of a substring in a string

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.
  • Comment on Re^4: Find the boundaries of a substring in a string

Replies are listed 'Best First'.
Re^5: Find the boundaries of a substring in a string
by hippo (Archbishop) on Jun 27, 2023 at 09:36 UTC

    In that case, subtract 1 from the end value and you are good to go:

    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) - 1; my $seg_start = $seg_end - $seg_length + 1; print $seg. "|" . $seg_start . "-" . $seg_end . "\n"; }
    $ perl 11153140.pl BBBBBBBBBBBB|9-20 BBBBBBBBBBBBBB|29-42 BBBBBBBBBBBBBB|63-76 BBBBBBBB|79-86 $

    🦛

Re^5: Find the boundaries of a substring in a string
by Anonymous Monk on Jun 27, 2023 at 09:35 UTC
    I added that and it works fine now, thank you!
    my $seg_end = pos ($seq)-1; my $seg_start = $seg_end - $seg_length+1;