Hi Experts,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.