Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: column counter and printf question

by scorpio17 (Canon)
on Oct 28, 2015 at 14:12 UTC ( #1146272=note: print w/replies, xml ) Need Help??


in reply to column counter and printf question

use strict; # for actual use, read from an open file handle line this: #while (my $line = <$input> { # for testing, read from special DATA file handle # (data will come from end of file, below DATA tag) while( my $line = <DATA>) { chomp $line; # remove end-of-line character $line =~ s/^\s+//; # strip leading whitespace next unless $line; # skip blank lines # skip first 12 lines next unless $. > 12; # $. contains current line number my @columns = split(/\s+/, $line); # split columns on whitespace my $col1 = shift @columns; # column 1 (throw away) my $col2 = shift @columns; # column 2 (throw away) my $col3 = shift @columns; # column 3 (special - keep this one) my $result = sprintf("%8.3f", $col3); # special format for col 3 # loop over remaining columns, appending to result string for my $c (@columns) { my $data = sprintf("%10.3f", $c); $result .= " $data"; # note the space character (ch +ange if required) } print "$result\n"; # print the final result (note + end of line character)" } # my guess at the input data __DATA__ SKIP 1 SKIP 2 SKIP 3 SKIP 4 SKIP 5 SKIP 6 SKIP 7 SKIP 8 SKIP 9 SKIP 10 SKIP 11 SKIP 12 Line_1 0.0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 Line_2 0.0 0.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 Line_3 0.0 0.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 1.0 Line_4 0.0 0.0 4.0 5.0 6.0 7.0 8.0 9.0 1.0 2.0 Line_5 0.0 0.0 5.0 6.0 7.0 8.0 9.0 1.0 2.0 3.0

update: fixed typo in "strip whitespace" regex (added '^' character)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1146272]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2019-10-18 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?