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

Printing first column value

P: 55
Hi All,

I have data file which has 6 column, I am trying to take the first value(-16.2577) of 6th column and substract it to the rest of the values of 6th column.
I split the file on space and stored in an array but the problem is I am not able to pick the first value and substract it in while loop.

DATA FILE:
-16.2262 -6.01803 -25.5829 -19.4488 -14.0128 -16.2577
-18.6505 -23.0397 -26.0685 -24.5328 -21.9307 -22.8444
-30.1621 -22.6838 -17.0961 -17.0979 -16.7653 -20.761
-21.5754 -21.2215 -13.6387 -12.3095 -17.8838 -17.3258
-21.7664 -25.4331 -22.6777 -23.1687 -24.755 -23.5602
-16.739 -13.0645 -28.5916 -19.7581 -24.1668 -20.464
-15.3542 -21.8463 -24.1616 -33.7034 -22.4266 -23.4984
-17.021 -22.1811 -23.1971 -11.4303 -29.3547 -20.6368
-25.7525 -15.5314 -20.4551 -19.9354 -26.7343 -21.6817
-12.4969 -9.64406 -23.1891 -18.2173 -18.8544 -16.4804
-22.9877 -26.167 -25.5358 -10.8135 -19.1759 -20.936
-17.3839 -14.6625 -12.1523 -16.8928 -28.1319 -17.8447
-14.5102 -15.1774 -21.9974 -24.3167 -27.1646 -20.6333
-12.6622 -17.8959 -17.2385 -17.1209 -18.1484 -16.6132
-23.6795 -23.24 -24.8987 -13.5031 -27.6614 -22.5965
-19.0322 -14.2305 -26.3313 -19.6006 -28.2886 -21.4966
-20.5431 -7.20871 -21.5272 -14.0325 -24.7013 -17.6026
-20.7694 -13.2671 -29.6587 -16.5918 -19.0846 -19.8743
-12.4844 -10.0744 -23.2201 -21.8157 -21.002 -17.7193
-20.9622 -15.8527 -22.7274 -28.2274 -28.6891 -23.2918
-24.8432 -11.8968 -24.58 -18.6088 -22.4424 -20.4742


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. open(A,"rot_1.dat");
  6. while (<A>)
  7. {
  8.     my @temp = split(/\s+/,$_);
  9.     my $a = $temp[5];
  10.     $a = $a - (-16.277);
  11.     print "$a\n";
  12. }
  13.  
Any help will be appriciated.

Thanks
Kumarboston
Oct 26 '09 #1
Share this Question
Share on Google+
1 Reply


Expert
P: 70
Try something like this:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $first_val;
  5. open my $fh, '<', 'rot_1.dat' or die "can not open rot_1.dat: $!";
  6. while (<$fh>) {
  7.     my $col6 = (split)[5];
  8.     $first_val = $col6 if $. == 1;  # first line
  9.     print $col6 - $first_val, "\n";
  10. }
  11.  
  12. __END__
  13. 0.0
  14. -6.5867
  15. -4.5033
  16. -1.0681
  17. -7.3025
  18. -4.2063
  19. -7.2407
  20. -4.3791
  21. -5.424
  22. -0.2227
  23. -4.6783
  24. -1.587
  25. -4.3756
  26. -0.355499999999999
  27. -6.3388
  28. -5.2389
  29. -1.3449
  30. -3.6166
  31. -1.4616
  32. -7.0341
  33. -4.2165
  34.  
To control the precision of your output, use printf.
Oct 27 '09 #2

Post your reply

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