473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1983
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

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

Similar topics

2
by: Mark | last post by:
How do I output formatted numerical data to an external (text) file. At a minimum I want to specify the number of decimal places to print. Ideally I'd like the type of control the fortran FORMAT...
8
by: Matthew Thorley | last post by:
Greetings, perhaps someone can explain this. I get to different styles of formatting for xmla and xmlb when I do the following: from elementtree import ElementTree as et xmla =...
4
by: Joe C | last post by:
I've written a console application and would like to isolate all screen output so that it will be easier to migrate the code to a GUI-type platform without modification to the base code. As a...
6
by: Jason Heyes | last post by:
I am starting to worry about the performance of formatted read/write operations on data-redundant objects in my program.What can I do to improve this performance should it become an issue? ...
2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
6
by: Magix | last post by:
Hi, I want to use fprintf to write to a file. My question about the formatted output How can I format so that I can allocate certain width for each %s (Left-aignlied) ? Example: fprintf("%s...
3
by: Craig Petrie | last post by:
Hi, I have a large table in Word 2003 that has formatted text in the cells and wish to read and convert a cells formatted contents to html output via vb.net code. The formatting contains the...
1
by: Tarun Mistry | last post by:
Hi everyone, is it possible to return the formatted interpretation of an XmlDocument object? Using an XmlWriter object I can write a formatted document to a file, however I want to store it...
6
by: Jojo | last post by:
Hi all, I was wondering how I can perform formatted output with C++ strings. For example, suppose I have in plain C: sprintf(C_string, "%5.2f %6d"); How can I do such a thing with C++...
4
by: keith | last post by:
I've been beating my head against this for a little while, so perhaps someone can help me out here? The code below should output exactly as follows (the hex data lines up in a fixed font): DATA...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.