PERL : Group & Sum in hash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL : Group & Sum in hash
# 1  
Old 01-13-2011
PERL : Group & Sum in hash

Hi,

I have a hash which is to be populated by reading data lines from a flat file. I am supposed to read fields 1-5 from the file and load them on to the hash such that fields 1-4 are going to be the hash key-set and field 5 is the hash value.

Field 5 is a monetary amount and is supposed to be summed for the same set of keys (fields 1-4).

This is how am doing the summation :
Code:
if (!$hash->{$field1}->{$field2}->{$field3}->{$field4} ) 
{
$hash->{$field1}->{$field2}->{$field3}->{$field4} = $field5;
}
else 
{
$hash->{$field1}->{$field2}->{$field3}->{$field4} = 
$hash->{$field1}->{$field2}->{$field3}->{$field4} + $field5;
}

This is working absolutely fine.
But the only thing which bothers me is - to process a file of 10000 data lines my code is taking more than a minute. Is there any other way which you think would be easier or would cost me less ?

Thanks.

Last edited by radoulov; 01-13-2011 at 06:09 AM.. Reason: Code tags, please!
sinpeak
# 2  
Old 01-13-2011
Quite a bit more information is needed to really answer this question. Can you concatentate fields one through four together to contruct a single hash? If they are fixed width this should work fine. If not you would need to get more creative. But the idea to combine the fields to create a single hash key. And you possibly need to be able to reverse the operation... that is, when given a hash key be able to recreate the four fields it comprises. You wind up with just one hash with one lengthy key. This would be faster than your complex structure.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

For Loop & SUM

pcmpath query device |awk 'BEGIN{print "DEVICE NAME\tSERIAL"} /DEVICE NAME/ {printf "%s\t", $5; getline; print substr($2, length($2)-3)}' This script returns output like below to pull out "DEVICE NAME SERIAL". ...... hdisk28 110B hdisk29 1112 hdisk30 1115 hdisk31 1116 hdisk32 1128... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

3. Shell Programming and Scripting

Sum up the column values group by using some field

12-11-2012,PNL,158406 12-11-2012,RISK,4564 12-11-2012,VAR_1D,310101 12-11-2012,VAR_10D,310101 12-11-2012,CB,866 12-11-2012,STR_VAR_1D,298494 12-11-2012,STR_VAR_10D,309623 09-11-2012,PNL,1024106 09-11-2012,RISK,4565 09-11-2012,VAR_1D,317211 09-11-2012,VAR_10D,317211 09-11-2012,CB,985... (7 Replies)
Discussion started by: manas_ranjan
7 Replies

4. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

7. Shell Programming and Scripting

Sum of column by group wise

Hello All , I have a problem with summing of column by group Input File - COL_1,COL_2,COL_3,COL_4,COL_5,COL_6,COL_7,COL_8,COL_9,COL_10,COL_11 3010,21,1923D ,6,0,0.26,0,0.26,-0.26,1,200807 3010,21,192BI ,6,24558.97,1943.94,0,1943.94,22615.03,1,200807 3010,21,192BI... (8 Replies)
Discussion started by: jambesh
8 Replies

8. Shell Programming and Scripting

Perform Hash sum

Hi Friends, I have a pipe delimited file with 20 million records. I want to perform the sum of the 13 field, for that I am using the code below, but the result is in exponential form. I want the number to be in floating number, so that I can use the number to comparision. Any help is highly... (3 Replies)
Discussion started by: anandapani
3 Replies

9. Shell Programming and Scripting

Column sum group by uniq records

Dear All, I want to get help for below case. I have a file like this. saman 1 gihan 2 saman 4 ravi 1 ravi 2 so i want to get the result, saman 5 gihan 2 ravi 3 like this. Pls help me. (17 Replies)
Discussion started by: Nayanajith
17 Replies
Login or Register to Ask a Question