in reply to Tallying overall frequency of characters in a set of strings by position

#!/usr/bin/perl # http://perlmonks.org/?node_id=1162755 use strict; use warnings; my %score; chomp(my @array = <DATA>); for my $i (1..@array) { $score{$1}[$-[0]] += 1/@array while $array[$i - 1] =~ /(.)/g; } printf " " . "%5s " x @array . "\n", sort keys %score; for my $pos ( 1..length $array[0] ) { printf "%1d" . "%7.2f" x @array . "\n", $pos, map { $score{$_}[$pos - 1] // 0 } sort keys %score; } __DATA__ AABBC BAABC AABBD AACBB
  • Comment on Re: Tallying overall frequency of characters in a set of strings by position
  • Download Code

Replies are listed 'Best First'.
Re^2: Tallying overall frequency of characters in a set of strings by position
by Anonymous Monk on May 11, 2016 at 15:40 UTC
    Thanks! Interesting approach. I don't quite understand the use of the $-[0] special variable here - would you mind explaining?

      $_[0] is the start position of the match in the string.

        Ah right, now I understand why it was used. When adding more entries to __DATA__ such as:

        __DATA__ AABBC BAABC AABBD AACBB AACBB AACBA
        It returns the error: Missing argument in printf at line 16, <DATA> line 6. I can't quite figure out how to alter the print statements at the end of the script to accommodate variable a number of entries.