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.