Perlseeker_1 has asked for the wisdom of the Perl Monks concerning the following question:

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

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

    G'day Perlseeker_1,

    This appears to be a follow-up question to printing output on text file from file - perl. It looks like you've made some modifications to the code I provided in Re: printing output on text file from file - perl.

    You've posted code which you say: "gives me this output: ..." - no it does not!

    You state "i am getting errors": where are they? are we supposed to guess what these errors are?

    Looking back over your last few posts, I see you've been pointed to "How do I post a question effectively?" on numerous occasions by various monks: please go and read that now.

    When you understand about making some effort to help us to help you, please read "How do I change/delete my post?".

    Now you'll be in a position to add whatever error and warning messages you're receiving, and to update your post with either the code that produces the output you've shown or the output that's produced by the code you've shown.

    Furthermore, if there's something you don't understand, you need to ask: we don't know what you don't know! For instance, looking at some of what's missing from your posted code, do you need to ask something about Inline::Files? [or, perhaps, do you just need to read its documentation?]

    When you're prepared to make some effort, you'll find we're more than happy to help.

    -- Ken

      Hi,

      The code which you provided, i am getting the correct output

      output i got from the code which you provided

      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

      But i am trying to print as below, where i am not able to

      i am trying to print the output as below

      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

      I need to add the sub for common id which comes under common type

      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

        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

Re: printing Hash references
by Anonymous Monk on Oct 20, 2013 at 05:26 UTC

    And your file inputs look like what? Atleast using module Inline::Files suggest that you have several file handles using are using. It would be proper to give how your input looks like