in reply to Re: Re: counting occurances
in thread counting occurances

Yet another version, trying for speed like davido's using   tr   , but without losing the speed to eval's just to force   tr   to accept the variables.   If this still doesn't go fast enough you'll have to unroll into 4 separate m/// statements.
my $temp = <DATA>; print "Discarding the file header line.\n"; print " ", $temp; my @chars = qw( A C G T ); my %cnts; while( <DATA> ) { foreach my $char (@chars) { # my $cnt = () = m/$char/g; # $cnts{$char} += $cnt; # printf " For char '%s' I found %d\n", $char, $cnt; $cnts{$char} += () = m/$char/g; } } foreach my $char (@chars) { printf " Char '%s': %6d\n", $char, $cnts{$char}; } __DATA__ Generated by a completely confused program yesterday ACGTGACTAGAGGCCCGGGGAAAAAAAAAACCCCCCC ACCTGACTAGAGGCCCGGGGAAAAAAAAAACCCCCCC ACGTGACTAGAGGCCCGGGGAAAAAAAAAACCCCCCC AGGTGAGTAGAGGGGGGGGGAAAAAAAAAAGGGGGGG ACGTGACTAGAGGCCCGGGGAAAAAAAAAACCCCCCC
Outputs
Discarding the file header line.
    Generated by a completely confused program yesterday
  Char 'A':      70
  Char 'C':      49
  Char 'G':      56
  Char 'T':      10

Replies are listed 'Best First'.
Re: Re: Re: Re: counting occurances
by shenme (Priest) on Sep 19, 2003 at 21:40 UTC
    Since we're spewing bilge today, thought I'd post what I'd been thinking to do to avoid the RE recompiles.   I know others have done it (many times) before, but ...
    my $magic = 'sub { '; foreach my $char (@chars) { $magic .= "\$cnts{$char} += ()= \$_[0] =~ m/$char/g;"; } $magic .= '}'; my $magicsub = eval "$magic"; die "eval to create anon sub failed '$@'" unless defined $magicsub; while( <DATA> ) { $magicsub->($_); }

    And in honor of the day:
    @pieces=split //, 'eight'; print "@pieces squawk!\n" for(1..2);