try something like:
$tcount;
$acount;
$gcount;
$ccount;
foreach $letter (@DNA)
{
if (($letter eq "t" ) || ($letter eq "T"))
{
$tcount++:
}
and so on
eggmatters
Junior Poster in Training
67 posts since Nov 2008
Reputation Points: 31
Solved Threads: 4
Unless you have to use an array you could also do the following.
#!/usr/bin/perl -w
#CountLetters.pl
use strict;
print "Enter string of letters: ";
chomp (my $input = <STDIN>);
my ($char, $count, $verb, $s);
print "\n";
while ($input =~ m/(.)/g) {
$char = $1;
#Count and remove all instances of this character -- lower and uppercase
$count = $input =~ s/$char//gi;
#Did we find more than one? Singular or plural?
$verb = $count == 1 ? "is" : "are";
$s = $count > 1 ? "s" : "";
print "There $verb $count '$char'$s.\n";
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
$input =~ s/$char/$char/gi would be more appropriate.
ithelp
Nearly a Posting Maven
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
If you need to store the results in a hash you could do it like this:
#!/usr/bin/perl -w
#CountLetters.pl
use strict;
print "Enter string of letters: ";
chomp (my $input = <STDIN>);
my ($char, $count, $verb, $s, %hash);
print "\n";
while ($input =~ m/(.)/) {
#When all characters have been counted and replaced with Null,
# $input will not match /(.)/ and the loop will end
$char = $1;
#Count and remove all instances of this character -- lower and uppercase
#Replace $char with Null so we won't count same character again
$count = $input =~ s/$char//gi;
#Did we find more than one? Singular or plural?
$verb = $count == 1 ? "is" : "are";
$s = $count > 1 ? "s" : "";
print "There $verb $count '$char'$s.\n";
$hash{$char} = $count; #Store $char as hash key and $count as hash value
}
print "\nThe letter counts have been stored in a hash.\n";
while (($char, $count) = each %hash ){
print "Hash key '$char' has hash value $hash{$char}\n";
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
Also, I think it should be noted that the moderators here frown upon folks writing out the answers to questions in code. Hash implementations and and just about anything else in Perl are pretty well documented online.
eggmatters
Junior Poster in Training
67 posts since Nov 2008
Reputation Points: 31
Solved Threads: 4