Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: array of arrays

by kevbot (Priest)
on Jun 13, 2017 at 05:33 UTC ( #1192655=note: print w/replies, xml ) Need Help??


in reply to Re^2: array of arrays
in thread array of arrays

Hello virtualweb,

My solution did not use an array. The contents of $data{$key} are a single scalar value (the running total of values for the given key). So, my code is not keeping track of how many data entries that are encountered for a given value of $key.

Here is a modified version of my code that will print out the information you request. Note, I'm still not using arrays. In this code, $data{$key} contains a hash reference with keys size and total. The value of size is the current number of elements found for the given $key. The value of total is the current total of values that have been encountered for the given $key.

#!/usr/bin/env perl use strict; use warnings; open my $fh, '<', 'test_file.txt' or die 'Can not open file'; my %data; my $row_number = 1; my $grand_total = 0; while(<$fh>){ chomp $_; my ($key, $val) = split(/\|/, $_); $data{$key}->{'size'}++; $data{$key}->{'total'} += $val; $grand_total += $val; print "Row ($row_number) ". "/ Array Size [$data{$key}->{'size'}] key ($key) ". "/ Amount ($val) / Sub Total ($data{$key}->{'total'}) ". "/ Grand_Total = ($grand_total)\n"; ++$row_number; } foreach my $key ( sort { $a <=> $b } keys %data ){ print "the total is: (".$data{$key}->{'total'}.")\n"; } exit;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1192655]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2019-06-17 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (80 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!