in reply to Re: printing Hash references
in thread printing Hash references

Hi Expert,

Mentioned earlier in my previous posts, that i am reading the data from text files and adding the common id's which comes under specific types

I am reading three files called file1, file2 and file3, the data in files are as....

Input file contains data as ...

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

code from perl monk by Kcott and i have modified

foreach my $file (@file_handles) { open FILE, "<$file" or die "file not exists\n"; while (chomp ( my ($id, $type, $val1, $val2, $val3, $val4) = split /\t +/, <FILE>) ) { $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";

using the code which you mentioned in your previous reply, i got the below output

Output i got

A 2 2 2 2 Sub 2 2 2 2 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 18 14 6 6

but i am trying to the output as below

A 2 2 2 2 Sub 2 2 2 2 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 10 10 6 6 TACK E 4 2 0 0 F 4 2 0 0 Sub 8 4 0 0 Tot 18 14 6 6

Is posted correctlty now, please tell and thanks for your guidance and yes i am keen to learn, please guide me

Rgeards

Jen

Replies are listed 'Best First'.
Re^3: printing Hash references
by kcott (Archbishop) on Oct 20, 2013 at 07:50 UTC

    Working on the basis that you've now posted actual code and all messages:

    Put the "use warnings;" line back in your code. Deal with the warning messages that you chose to hide by removing it. If you don't understand the messages, use the diagnostics pragma.

    Put the "use strict;" line back in your code.

    In order to get "Tot1 ..." in your output, you'll need print. You've used this to generate other output, so you should know how to use it. It looks like this is conditional: use an if statement or, if you don't know how to do that, see "perlintro -- a brief introduction and overview of Perl".

    -- Ken