Brian Fraser wrote:
On Wed, Dec 1, 2010 at 4:58 PM, Mariano Loza Coll<[email protected]>wrote:
Alternatively, a good samaritan out there can gelp you with a one or
two-liner code.

If you only want to count letters, here's the 1-2 liner code, I guess:

#! /usr/bin/perl
use strict;
use warnings;
use 5.010;

my %letter_count;
my %regex_count;
my $total;

while (<DATA>) {
     chomp;
     /(.)(??{ $regex_count{$1}++; $total++ })/g; #Using a regex

     for my $letter (split //) {       #Using a for loop
         next if $letter =~ /\s/;

If you are going to skip whitespace characters anyway then just do:

        for my $letter ( /\S/g ) {

Which will create a list of only non-whitespace characters.


         $letter_count{$letter}++;
     }
}

printf "%s\t=>\t%f\n", $_, ($regex_count{$_} / $total) * 100 for sort keys
%regex_count;


__DATA__
MGSSKSKPKDPSQRRRSLEPPDSTHHGGFPASQTPNKTAAPDTHRTPSRSFGTVATEPKLFGGFNTSDTV
TSPQRAGALAGGVTTFVALYDYESWIETDLSFKKGERLQIVNNTEGNWWLAHSLTTGQTGYIPSNYVAPS
DSIQAEEWYFGKITRRESERLLLNPENPRGTFLVRESETTKGAYCLSVSDFDNAKGLNVKHYKIRKLDSG
GFYITSRTQFSSLQQLVAYYSKHADGLCHRLTNVCPTSKPQTQGLAKDAWEIPRESLRLEVKLGQGCFGE
VWMGTWNGTTRVAIKTLKPGTMSPEAFLQEAQVMKKLRHEKLVQLYAVVSEEPIYIVIEYMSKGSLLDFL
KGEMGKYLRLPQLVDMAAQIASGMAYVERMNYVHRDLRAANILVGENLVCKVADFGLARLIEDNEYTARQ
GAKFPIKWTAPEAALYGRFTIKSDVWSFGILLTELTTKGRVPYPGMGNGEVLDRVERGYRMPCPPECPES
LHDLMCQCWRRDPEERPTFEYLQAQLLPACVLEVAE


The regex solution uses the (??{CODE}) extended regular expression,
explained in perlre[0]. The for solution uses split with an empty pattern,
which splits on the empty string[1].

[0] http://perldoc.perl.org/perlre.html#Extended-Patterns
[1] http://perldoc.perl.org/functions/split.html



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to