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

Hi all, I have some code that I can't get working. The code essentially sorts through an array and counts the occurrences of a particular pattern. The frequency of occurrences of each pattern are then inputted into a hash with the pattern as its key. Here is the code:
The arrays contain data similar to this: @truncated_sequences = qw(JFADLKGJLFLGJGAFDJ DSJLJFLDJSLF...E.T.C); @triamino_combo = qw(AAA AAB AAC AAD.......e.t.c); foreach $prot_string (@truncated_sequences) { foreach $tri_seq (@triamino_combo) { while ($prot_string =~ /$tri_seq/g) { $tri_freq{$tri_seq}++; } } }
This works fine and gets it right, but I also want the patterns that do not occur to be entered into the hash with a value of 0. I've tried to add the following code but it seems to mess up the counts of the matched patterns:
foreach $prot_string (@truncated_sequences) { foreach $tri_seq (@triamino_combo) { if ($prot_string =~ /$tri_seq/g) { $tri_freq{$tri_seq}++; } else { $tri_freq{$tri_seq} = "0"; } } }
Any help would be appreciated, thanks

Replies are listed 'Best First'.
Re: manipulating hashes
by thelenm (Vicar) on Nov 19, 2002 at 19:14 UTC
    The counts get messed up because you changed the while loop to an if statement... to get 0 for the unseen combinations, just assign 0 before the while loop, like this:
    foreach $prot_string (@truncated_sequences) { foreach $tri_seq (@triamino_combo) { $tri_freq{$tri_seq} = 0; while ($prot_string =~ /$tri_seq/g) { $tri_freq{$tri_seq}++; } } }

    -- Mike

    --
    just,my${.02}

      Then you're starting back at 0 for each $prot_string... I'm not sure that's what you want...You can also use index()
      foreach $prot_string (@truncated_sequences) { foreach $tri_seq (@triamino_combo) { $i=-1; while(($i=index($prot_string,$tri_seq,$i)) > -1){ $tri_freq{$tri_seq}++; $i+=3; } $tri_freq{$tri_seq}='0' unless defined $tri_freq{$tri_seq}; } }
Re: manipulating hashes
by Thelonius (Priest) on Nov 19, 2002 at 19:31 UTC
    The arrays contain data similar to this: @truncated_sequences = qw(JFADLKGJLFLGJGAFDJ DSJLJFLDJSLF...E.T.C); @triamino_combo = qw(AAA AAB AAC AAD.......e.t.c); $tri_freq{$_} = 0 for @triamino_combo; # <==== NOTE foreach $prot_string (@truncated_sequences) { foreach $tri_seq (@triamino_combo) { while ($prot_string =~ /$tri_seq/g) { $tri_freq{$tri_seq}++; } } }
Re: manipulating hashes
by John M. Dlugosz (Monsignor) on Nov 19, 2002 at 19:26 UTC
    And you don't have to use a string "0", you can use numeric 0.