Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by supriyoch_2008 (Scribe)
on May 01, 2012 at 13:19 UTC ( #968241=perlquestion: print w/ replies, xml ) Need Help??
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).

The code that I have used goes as follows:

#!usr/bin/perl-w my $seq="ATCATCATCATC"; my (%first,%second,%third); my @trilet = $seq =~ /.../g; # Line 4 # Line 5 Perlsyn LOOP foreach my $letters ('AT','TC','AC') { #init $first{ $letters }=0; $second{ $letters }=0; $third{ $letters }=0;# Line 9 } foreach my $tri (@trilet) { # Line 13 perlfunc : substr $first{ substr $tri,0,2 }++; $second{ substr $tri,1,2 }++; # Line 14 $third{ substr $tri,2,2 }++; # Line 15 } foreach my $letters ('AT','TC','AC') { print" Letters At 1st & 2nd Positions:\n"; print " $letters=$first{$letters}; "; # Line 19 } print "\n\n"; foreach my $letters ('AT','TC','AC') { print" Letters At 2nd & 3rd Positions:\n"; print " $letters=$second{$letters}; ";# Line 24 } print "\n\n"; # Line 25 foreach my $letters ('AT','TC','AC') { print" Letters At 1st & 3rd Positions:\n"; print " $letters=$third{$letters}; ";# line 28 } print "\n\n";# Line 30 exit;

I have got the following wrong result for "Letters at 1st & 3rd Positions" in last line of result for AC i.e. AC=0; it should be AC=4

C:\Users\xyz\Desktop>chak.pl Letters At 1st & 2nd Positions: AT=4; Letters At 1st & 2nd Positions: TC=0; Letters At 1st & 2nd Positions: AC=0; Letters At 2nd & 3rd Positions: AT=0; Letters At 2nd & 3rd Positions: TC=4; Letters At 2nd & 3rd Positions: AC=0; Letters At 1st & 3rd Positions: AT=0; Letters At 1st & 3rd Positions: TC=0; Letters At 1st & 3rd Positions: AC=0;(It is wrong; 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?
Select or Download Code
Re: Why am I getting wrong result in counting the number and kind of 2-letter in 3-letter words in a string?
by JavaFan (Canon) on May 01, 2012 at 13:35 UTC
    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:

    foreach my $tri (@trilet) { my ($f, $s, $t) = $tri =~ /(.)(.)(.)/; $first{"$f$s"}++; $second{"$s$t"}++; $third{"$f$t"}++; }
Re: Why am I getting wrong result in counting the number and kind of 2-letter in 3-letter words in a string?
by toolic (Chancellor) on May 01, 2012 at 13:39 UTC
    Add this to your code:
    use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\%third);

    Does this output look right to you?:

    $VAR1 = { 'AC' => 0, 'AT' => 0, 'C' => 4, 'TC' => 0 };

    If not, I agree that Line 15 is not what you want. See also Basic debugging checklist. How about:

    $third{ substr($tri,0,1) . substr($tri,2,1) }++; # Line 15
Re: Why am I getting wrong result in counting the number and kind of 2-letter in 3-letter words in a string?
by johngg (Abbot) on May 01, 2012 at 13:57 UTC

    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:-

    knoppix@Microknoppix:~$ perl -E ' > $trilet = q{AGT}; > say $trilet; > $ss02 = substr $trilet, 0, 2; > say $ss02; > $ss12 = substr $trilet, 1, 2; > say $ss12; > $ss22 = substr $trilet, 2, 2; > say $ss22;' AGT AG GT T knoppix@Microknoppix:~$

    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.

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-09-17 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls