in reply to Counting using transliteration

First off, are you sure that the line has any A's in it? Because you have an error in the way you get your lines. Try adding a print "$line\n"; statement inside your loop, and try to open a file with several lines. It will only print every second line, namely line 2, 4, 6 etc. This is because when you do while(<MYFILE>) it assigns a line to $_, and then you assign the next line to $line. Try adding the line print "$_\n"; before the other print statement, and try again - now it will print all the lines.

The solution is of course to either write the loop like this:

while( my $line = <MYFILE> ) { ... }
or to use the special var $_ instead, and remove the assignment to $line. Like so:
$Acount = tr/A//;

Hope this will help.


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Replies are listed 'Best First'.
Re: Re: Counting using transliteration
by basm100 (Novice) on Feb 25, 2002 at 14:06 UTC
    Thanq for the help guys. I don't think I've explained what I'm wanting to do properly. I am wanting to count the number of each type of character in a line, and then (eventually) find a way to print what the most common character was. This should generate a consensus sequence for my multiple alignments. (Im a bioinformatician)

    So for example, say the line is ---ADGRCMNAAAPPRS I want the program to count the number of the different characters: $Acount, $Dcount, $gapcount (which is -) etc. and store the most common letter in my consensus sequence array. Think this is possible ?! Then the program should move onto the next line of the file and repeat the process... Thanks for pointing out I don't need to put $line, I realise now that the line information is stored in $_

    basm100

      while ($l=<DATA>) { %count = (); $count{$1}++ while ($l =~ /(.)/g); $max = 0; $max_key; foreach (keys %count) { $max = $count{$_}, $max_key = $_ if ($count{$_} > $max +); } push @consensus, $max_key; } print "@consensus\n"; __END__ ---ADGRCMNAAAPPRS ASAMNP-PRCM--MWQZ TYYCYLCKLKDUEAOLE $ perl cons.pl A - Y