Perl

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 2
Reputation: wannagethelp is an unknown quantity at this point 
Solved Threads: 0
wannagethelp wannagethelp is offline Offline
Newbie Poster

Perl

 
0
  #1
Oct 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: eggmatters is an unknown quantity at this point 
Solved Threads: 4
eggmatters eggmatters is offline Offline
Junior Poster in Training
 
0
  #2
Oct 23rd, 2009
try something like:
  1. $tcount;
  2. $acount;
  3. $gcount;
  4. $ccount;
  5. foreach $letter (@DNA)
  6. {
  7. if (($letter eq "t" ) || ($letter eq "T"))
  8. {
  9. $tcount++:
  10. }
  11. and so on
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 52
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 7
d5e5's Avatar
d5e5 d5e5 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 24th, 2009
Unless you have to use an array you could also do the following.
  1. #!/usr/bin/perl -w
  2. #CountLetters.pl
  3. use strict;
  4. print "Enter string of letters: ";
  5. chomp (my $input = <STDIN>);
  6. my ($char, $count, $verb, $s);
  7. print "\n";
  8. while ($input =~ m/(.)/g) {
  9. $char = $1;
  10. #Count and remove all instances of this character -- lower and uppercase
  11. $count = $input =~ s/$char//gi;
  12. #Did we find more than one? Singular or plural?
  13. $verb = $count == 1 ? "is" : "are";
  14. $s = $count > 1 ? "s" : "";
  15. print "There $verb $count '$char'$s.\n";
  16. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,868
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso
 
0
  #4
Oct 25th, 2009
$input =~ s/$char/$char/gi would be more appropriate.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: wannagethelp is an unknown quantity at this point 
Solved Threads: 0
wannagethelp wannagethelp is offline Offline
Newbie Poster
 
0
  #5
Oct 25th, 2009
hi everyone
thnx a lot ,,, but if i want to use a hash ( letter as a key and number as value ) how it would be written ..
thnx again
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 52
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 7
d5e5's Avatar
d5e5 d5e5 is offline Offline
Junior Poster in Training
 
0
  #6
Oct 25th, 2009
If you need to store the results in a hash you could do it like this:
  1. #!/usr/bin/perl -w
  2. #CountLetters.pl
  3. use strict;
  4. print "Enter string of letters: ";
  5. chomp (my $input = <STDIN>);
  6. my ($char, $count, $verb, $s, %hash);
  7. print "\n";
  8.  
  9. while ($input =~ m/(.)/) {
  10. #When all characters have been counted and replaced with Null,
  11. # $input will not match /(.)/ and the loop will end
  12. $char = $1;
  13. #Count and remove all instances of this character -- lower and uppercase
  14. #Replace $char with Null so we won't count same character again
  15. $count = $input =~ s/$char//gi;
  16. #Did we find more than one? Singular or plural?
  17. $verb = $count == 1 ? "is" : "are";
  18. $s = $count > 1 ? "s" : "";
  19. print "There $verb $count '$char'$s.\n";
  20. $hash{$char} = $count; #Store $char as hash key and $count as hash value
  21. }
  22. print "\nThe letter counts have been stored in a hash.\n";
  23. while (($char, $count) = each %hash ){
  24. print "Hash key '$char' has hash value $hash{$char}\n";
  25. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: vbharathi is an unknown quantity at this point 
Solved Threads: 1
vbharathi vbharathi is offline Offline
Newbie Poster
 
0
  #7
Oct 26th, 2009
  1. #!/usr/bin/perl
  2. use Data::Dumper;
  3.  
  4. print "Enter String:\n";
  5. my $string=<STDIN>;
  6. chomp($string);
  7. %hash;
  8. my @a=split(//,$string);
  9. foreach(@a)
  10. {
  11. if(defined $hash{$_})
  12. {
  13. $i=$hash{$_};
  14. $i++;
  15. $hash{$_} = $i;
  16. }
  17. else{
  18. $i=1;
  19. $hash{$_}=$i;
  20. }
  21.  
  22. }
  23. print Dumper \%hash;exit;
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: eggmatters is an unknown quantity at this point 
Solved Threads: 4
eggmatters eggmatters is offline Offline
Junior Poster in Training
 
0
  #8
Oct 26th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: wich is an unknown quantity at this point 
Solved Threads: 0
wich wich is offline Offline
Newbie Poster
 
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.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Perl Forum


Views: 668 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC