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

Thanks! Interesting approach. I don't quite understand the use of the $-[0] special variable here - would you mind explaining?
  • Comment on Re^2: Tallying overall frequency of characters in a set of strings by position

Replies are listed 'Best First'.
Re^3: Tallying overall frequency of characters in a set of strings by position
by Anonymous Monk on May 11, 2016 at 16:17 UTC

    $_[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.

        Oops

        #!/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 (keys %score) . "\n", sort keys %score; for my $pos ( 1..length $array[0] ) { printf "%1d" . "%7.2f" x (keys %score) . "\n", $pos, map { $score{$_}[$pos - 1] // 0 } sort keys %score; } __DATA__ AABBC BAABC AABBD AACBB AACBB AACBA