By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,692 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,692 IT Pros & Developers. It's quick & easy.

Format problem

P: 37
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
Share this question for a faster answer!
Share on Google+

Post your reply

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