| | |
Perl
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
Hallo everyone
im starting to learn perl and i have a problem , i have an exercise asking me to count all the letters each apart in a DNA string , for example
my @DNA =( ctagctagcatgacgatacatgacagataggatacagatagacagatacagatacagatacagatagacccatgacagatac)
so i have to make a perl script showing the user how many t's , a's , c's and g's he typed , what a mean , if the user of the perl script type CTGACTGACGTACGTACGTA , the perl script have to show him how many a's t's c's and g's he typed ....
hoping for help ,,, thanks a lot
im starting to learn perl and i have a problem , i have an exercise asking me to count all the letters each apart in a DNA string , for example
my @DNA =( ctagctagcatgacgatacatgacagataggatacagatagacagatacagatacagatacagatagacccatgacagatac)
so i have to make a perl script showing the user how many t's , a's , c's and g's he typed , what a mean , if the user of the perl script type CTGACTGACGTACGTACGTA , the perl script have to show him how many a's t's c's and g's he typed ....
hoping for help ,,, thanks a lot
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 4
0
#2 Oct 23rd, 2009
try something like:
Perl Syntax (Toggle Plain Text)
$tcount; $acount; $gcount; $ccount; foreach $letter (@DNA) { if (($letter eq "t" ) || ($letter eq "T")) { $tcount++: } and so on
0
#3 Oct 24th, 2009
Unless you have to use an array you could also do the following.
Perl Syntax (Toggle Plain Text)
#!/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"; }
0
#4 Oct 25th, 2009
$input =~ s/$char/$char/gi would be more appropriate.
0
#6 Oct 25th, 2009
If you need to store the results in a hash you could do it like this:
Perl Syntax (Toggle Plain Text)
#!/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"; }
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 1
0
#7 Oct 26th, 2009
Perl Syntax (Toggle Plain Text)
#!/usr/bin/perl use Data::Dumper; print "Enter String:\n"; my $string=<STDIN>; chomp($string); %hash; my @a=split(//,$string); foreach(@a) { if(defined $hash{$_}) { $i=$hash{$_}; $i++; $hash{$_} = $i; } else{ $i=1; $hash{$_}=$i; } } print Dumper \%hash;exit;
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#9 Oct 29th, 2009
i am the biggest fan of this forum its so useful for us. thanks for this useful posting i like it very much.
![]() |
Similar Threads
- Good books on Shell Programming/Perl (Perl)
- perl developer- job vacancy (Web Development Job Offers)
- Perl Developer with Messaging (Software Development Job Offers)
- Looking for perl programmer (Web Development Job Offers)
- where to start with perl? (Perl)
- Your thoughts regarding Perl (Perl)
- OptiPerl - Perl IDE (Perl)
Other Threads in the Perl Forum
- Previous Thread: Perl: how to replace character '/' with '\'
- Next Thread: Parsing of information in perl
Views: 669 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Perl






