468,321 Members | 1,654 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Formatted output for string

Hi All,
I have a sequence of character like "ACDFGEYRTWJSHDGKSKSDHFCNDHD" and I am trying to format in a way such that the output looks like this
A C D F G E Y R T W
J S H D G K S K S D
H F C N D H D

ie, one space between each character and 10 character in one line.
I am getting the sequence in tab separated list but not able to format 10 character per line with one spacing.
Any help will be appreciated.

Thanks
Kumar
Aug 21 '08 #1
4 1814
nithinpes
410 Expert 256MB
What have you tried so far?
All you need to do is to split the string into characters and use the indices of resulting array for character count to format it accordingly...
Aug 21 '08 #2
Thanks for the reply,
here is my code
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my %one2three = ( 'G'=>'GLY','A'=>'ALA','V'=>'VAL','L'=>'LEU','I'=>'ILE',          
  5.                   'S'=>'SER','T'=>'THR','C'=>'CYS','M'=>'MET','P'=>'PRO',          
  6.                   'D'=>'ASP','N'=>'ASN','E'=>'GLU','Q'=>'GLN','K'=>'LYS',          
  7.                   'R'=>'ARG','H'=>'HIS','F'=>'PHE','Y'=>'TYR','W'=>'TRP',          
  8.                   'B'=>'ASX','Z'=>'GLX','X'=>'UNK');
  9. my $count = 0;my @val;my ($line,$seq);
  10.  
  11. open (FH, "GHHDP") or die "Check for the file";
  12. my @prot = <FH>;
  13. close FH;
  14.  
  15. foreach $line (@prot)
  16. {
  17.     if($line =~/^\s*#/)
  18.        {next;}
  19.     elsif($line =~/^>(.*)/)
  20.        { my $title = $1;
  21.          print "* $title\n*\n";
  22.      }
  23.     else{$seq .= $line;}
  24. }
  25. $seq=~ s/\s//g;
  26. my @ss = split(//,$seq);
  27. foreach my $rr(@ss)
  28. {
  29.      $rrr = $one2three{$rr};
  30.      print "$rrr\t";
  31.      $count +=1;
  32. }
  33. print "$count\n";
  34.  
thanks
Kumar
Aug 21 '08 #3
nithinpes
410 Expert 256MB
You are printing out three-letter representations of the amino acids and not single characters as in your initial posting. However, logic remains the same. You are formatting your output with tab(\t). Replace that with a single space.

Expand|Select|Wrap|Line Numbers
  1. foreach my $rr(@ss)
  2. {
  3.      my  $rrr= $one2three{$rr};
  4.      print "$rrr ";   ##replace \t with single space
  5.      $count +=1;
  6.      print "\n" if($count%10==0); ## insert newline after every 10 records
  7. }
  8.  
- Nithin
Aug 21 '08 #4
Thanks Nithin for the reply,
I was getting confused by using the sprintf function but now its working fine. :)

Thanks
Kumar
Aug 21 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Mark | last post: by
8 posts views Thread by Matthew Thorley | last post: by
4 posts views Thread by Joe C | last post: by
2 posts views Thread by Steven T. Hatton | last post: by
6 posts views Thread by Magix | last post: by
3 posts views Thread by Craig Petrie | last post: by
1 post views Thread by Tarun Mistry | last post: by
6 posts views Thread by Jojo | last post: by
4 posts views Thread by keith | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.