I am new to perl hash references, i am trying to print the out put but i am getting errors
#!/usr/bin/env perl -l use strict; use warnings; use Inline::Files; my @file_handles = \(*file1, *fil2, *file3); my %data; for my $fh (@file_handles) { while (<$fh>) { $data{in}{$type}{$id}{val1} += $val1; $data{in}{$type}{$id}{val2} += $val2; $data{in}{$type}{$id}{val3} += $val3; $data{in}{$type}{$id}{val4} += $val4; $data{subtotal}{$type}{val1} += $val1; $data{subtotal}{$type}{val2} += $val2; $data{subtotal}{$type}{val3} += $val3; $data{subtotal}{$type}{val4} += $val4; $data{total}{val1} += $val1; $data{total}{val2} += $val2; $data{total}{val3} += $val3; $data{total}{val4} += $val4; } } close FILE; for my $type (sort keys %{$data{in}}) { print "$type\n"; for (sort keys %{$data{in}{$type}}) { print join("\t" => $_, @{$data{in}{$type}{$_}}{qw{val1 val2 va +l3 val4}})."\n"; } print join("\t" => 'Sub', @{$data{subtotal}{$type}}{qw{val1 val2 v +al3 val4}})."\n"; } print join("\t" => 'Tot', @{$data{total}}{qw{val1 val2 val3 val4}})."\ +n";
gives me this output:
CACK A 2 2 2 2 B 2 2 2 2 Sub 4 4 4 4 KACK A 2 2 0 0 B 2 2 0 0 Sub 4 4 0 0 RACK A 2 2 2 2 B 2 2 2 2 Sub 4 4 4 4 TACK E 4 2 0 0 F 4 2 0 0 Sub 8 4 0 0 Tot 20 16 8 8
Input files
file 1 A RACK 2 2 2 2 B RACK 2 2 2 2 A CACK 2 2 2 2 B CACK 2 2 2 2 file 2 A KACK 2 2 B KACK 2 2 file 3 E TACK 4 2 F TACK 4 2
I'm trying to sum sub for same id's which comes under same type and was expecting output as
CACK A 2 2 2 2 B 2 2 2 2 Sub 4 4 4 4 KACK A 2 2 0 0 B 2 2 0 0 Sub 4 4 0 0 RACK A 2 2 2 2 B 2 2 2 2 Sub 4 4 4 4 Tot1 12 12 8 8 TACK E 4 2 0 0 F 4 2 0 0 Sub 8 4 0 0 Tot 20 16 8 8
Tot1 should sum the value of sub for CACK,KACK and RACK and Tot should sum the values of all subs
please guide me on this,thanks in advance
In reply to printing Hash references by Perlseeker_1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |