Printing first column value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumarboston
    New Member
    • Sep 2007
    • 55

    Printing first column value

    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


    [CODE=perl]
    #!/usr/bin/perl
    use strict;
    use warnings;

    open(A,"rot_1.d at");
    while (<A>)
    {
    my @temp = split(/\s+/,$_);
    my $a = $temp[5];
    $a = $a - (-16.277);
    print "$a\n";
    }
    [/CODE]

    Any help will be appriciated.

    Thanks
    Kumarboston
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    Try something like this:

    Code:
    use strict;
    use warnings;
    
    my $first_val;
    open my $fh, '<', 'rot_1.dat' or die "can not open rot_1.dat: $!";
    while (<$fh>) {
        my $col6 = (split)[5];
        $first_val = $col6 if $. == 1;  # first line
        print $col6 - $first_val, "\n";
    }
    
    __END__
    0.0
    -6.5867
    -4.5033
    -1.0681
    -7.3025
    -4.2063
    -7.2407
    -4.3791
    -5.424
    -0.2227
    -4.6783
    -1.587
    -4.3756
    -0.355499999999999
    -6.3388
    -5.2389
    -1.3449
    -3.6166
    -1.4616
    -7.0341
    -4.2165
    To control the precision of your output, use printf.

    Comment

    Working...