Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: part - split up files according to column value

by sl7020 (Initiate)
on Jul 10, 2012 at 21:26 UTC ( [id://980949]=note: print w/replies, xml ) Need Help??


in reply to Re: part - split up files according to column value
in thread part - split up files according to column value

Monks: Trying to modify script to give me a subtotal of column 14 (see below.)

while (<>) { s/\r?\n$//; my @c = split /$sep/o; my $sub_total = 0; $sub_total += $c[14]; my $key = join $sep, @c[ @col ]; if (not defined $lines{ $key }) { $lines{ $key } ||= []; }; push @{ $lines{$key}}, $_ push @{ $totals{$key}}, $sub_total }

My code is failing with the following: "my" variable %lines masks earlier declaration in same scope at ./part-v2.pl line 179. "my" variable $key masks earlier declaration in same statement at ./part-v2.pl line 180. syntax error at ./part-v2.pl line 176, near "$_ push" syntax error at ./part-v2.pl line 176, near "}}" Global symbol "$sub_total" requires explicit package name at ./part-v2.pl line 177.

syntax error at ./part-v2.pl line 191, near "}" Execution of ./part-v2.pl aborted due to compilation errors.

========================

Any help would be appreciated.

Replies are listed 'Best First'.
Re^3: part - split up files according to column value
by aaron_baugher (Curate) on Jul 10, 2012 at 22:39 UTC

    Statements end with semicolons. Blocks do not:

    } push @{ $lines{$key}}, $_; push @{ $totals{$key}}, $sub_total; }

    You also have a problem in that you instantiate $sub_total with a value of zero every time through the loop, right before you add a value to it, so it will always equal that value. This is probably not what you want. You should move the my line that creates it to before your loop, so it can accumulate inside the loop.

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found