supriyoch_2008 has asked for the
wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

I have a small string my $seq="ATCATCATCATC"; consisting of four 3-letter word i.e. ATC. For all the 3-letter words
I am interested in counting the number and the kind of 2-letter at 1st & 2nd positions (AT) of all 3-letter words, similarly
for 2nd & 3rd positions (TC) and for 1st & 3rd positions (AC). I have written the following code which gives correct result
for 1st & 2nd positions (AT=4) and for 2nd & 3rd positions (TC=4). But I am getting wrong result in counting the number and
kind of Letters at 1st & 3rd positions (i.e. AC=0; it should be AC=4).

I shall be glad if any perlmonk can help me correct the mistake (possibly at Line 15) and further modify the code
so that each result comes out after being assigned to a separate scalar variable (like $AT12 for AT at 1st & 2nd positions,
similarly $TC12, $AC12 . . . $TC13,$AC13. In the given code, all results come out in a lot.

Comment on Why am I getting wrong result in counting the number and kind of 2-letter in 3-letter words in a string?

I hope you don't mind if I offer a little advice. If your program is not doing quite what you expected and you think the problem might lie with a particular part of the code but you can't work out why, don't persist in trying to fix the program. Instead, break that code out into a smaller script or one-liner and run it to see if it is doing what you expected, perhaps something like this:-

It would immediately become obvious to you from the last line of the output that substr $trilet, 2, 2 is not doing what you expected. That would be the time to explore the documentation and read up on the substr function. This is a far less frustrating approach than trying to work out exactly what has gone wrong in your more complex script.

You seem to misunderstand substr. The second argument is the start index to take the sub string from, the third argument the length.

If you have to take the first and third character of a string, you cannot do that with a single sub string, as you are not interested in the second character. Perhaps you want to do something along the lines of:

Went to join the gridlock to see it
Held an eclipse party
Watched a live feed
I cn"t see tge kwubosd to amswr thus
I tried to see it, but 8000 miles of rock got in the way
What eclipse?
Wanted to see it, but they wouldn't reschedule it
Read the book instead