467,146 Members | 1,006 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

Format problem

Hello All,

I would like to replace the line in the my text file mathematically as follows,

TRANS -20.0 -30.0 -40.0

There are 4 blank spaces before TRANS and again 4 blank spaces between the numbers.

I use the code and get the following output

TRANS -13.0 -28.0 -31.0
Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl
  2. use warnings;
  3. use strict;
  4. open IN,"+< z.txt"; 
  5. open OUT,"> t.txt"; 
  6. while (<IN>) {
  7. $file =~ s/\s+(\S+)\s+(-?\d+\.?\d?)\s+(-?\d+\.?\d?)\s+(-?\d+\.?\d?)/printf "    $1    %.1f    %.1f     %.1f", $2+7,$3+2,$4+9/eg;
  8. print OUT;
  9. }  
1. As there are spaces in the input, I have shown the spaces in printf statement. Is this the correct way of representing it.

2. My other question is that if there are positive numbers in the input for eg

TRANS 20.0 30.0 40.0

I would like to have the numbers fixed in the respective position as this text file forms the input for other external programs. Therefore I need to maintain the position of the numbers like 20.0 and the last 0 should always be in 17th column and also 40.0 and the last zero must be in 37th column. If there are negative numbers as in the earlier case, the sign should be shown without altering the position of numbers. Is there anyway of representing this.

Aug 18 '08 #1
  • viewed: 815

Post your reply

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

Similar topics

2 posts views Thread by Bernd Lambertz | last post: by
6 posts views Thread by Dario Di Bella | last post: by
1 post views Thread by Wayne Aprato | last post: by
2 posts views Thread by Pat | last post: by
9 posts views Thread by insomniux | last post: by
3 posts views Thread by karthikla@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.