Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Perl script for the post processing of one CSV file

by BillKSmith (Monsignor)
on Oct 03, 2019 at 17:52 UTC ( [id://11107016]=note: print w/replies, xml ) Need Help??


in reply to Perl script for the post processing of one CSV file

A "quick and dirty" way to do this task is to use the perl Command Switches "-F" to do the input, main loop, and splitting. Use an END block to print the summary. Note that the variable @sums must be a declared as a package variable in order to access it in the END block. (Anyone know a better way?)
#!perl -F',' use strict; use warnings; use feature 'state'; state $header = 1; our @sums; if ($header) { printf "%-24s" . " %4s"x10, 'Number', @F[1..10]; $header = 0; next; } chomp $F[10]; for (reverse 2..10) { $F[$_] = ( ( $F[$_-1] xor $F[$_] ) ? .5 : 0 ); $sums[$_] += $F[$_]; } $F[1] = 0; $sums[1] += $F[1]; printf "%-24s " . " %3.1f "x10 . "\n", @F; END{ our @sums; printf '*'x75 . "\n"; printf "%-24s" . " %3.1f "x10 . "\n", "Sum of weights", @sums[1..10]; printf '*'x75 . "\n"; }
Bill

Replies are listed 'Best First'.
Re^2: Perl script for the post processing of one CSV file
by FreeBeerReekingMonk (Deacon) on Oct 03, 2019 at 21:13 UTC
    the second "our @sums" inside the END block is not needed. "our" already makes is accessible to all.
    And if the first declaration was "my @sums", then it can not be used by the END block, not even as @main::sums

      the second "our @sums" inside the END block is not needed

      Good point! I have often needed a second 'our' in a BEGIN block at the start of a file when using "-n". I never noticed that an END block at the end is included in the scope of the first 'our'. Note that if we change the single 'our' to 'my', the code still compiles without error. It fails to execute correctly due to life-time issues, not scope.

      Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-25 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found