The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Hash Harikrishna Shell Programming and Scripting 1 06-04-2008 04:03 AM
Perl Hash Harikrishna Shell Programming and Scripting 1 06-02-2008 08:45 PM
Hash in perl Harikrishna Shell Programming and Scripting 1 06-02-2008 01:00 AM
hash,array and perl new2ss Shell Programming and Scripting 3 05-23-2007 08:30 AM
Compare 2 hash deepakpv Shell Programming and Scripting 1 03-09-2007 12:34 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-02-2008
Registered User
 

Join Date: Jan 2008
Location: Chicago
Posts: 6
Urgent Help: Perl Hash compare

Hi,

I am new to Perl and any help in fixing my issue would be highly appreciated.

I have a hash reference with the below entries

IL 100.25
MN 200.25
MO 300
WI 100
IL 200
MN 300

I have to write a script that will compare the keys within the hash and generate a new hash with unique entries of states with the cumulative values. I mean the new has should have a single entry for each state with the value field summed up together.

Eg:

IL 300.25
MN 500.25
MO 300

If there are multiple entries for the same state, their values should be summed up and a new hash should be created. If there is a single entry for a state, that should be retained in the new hash.

Please help me with this.

Thanks,
Jamie
Reply With Quote
Forum Sponsor
  #2  
Old 01-02-2008
Registered User
 

Join Date: Jan 2008
Location: Chicago
Posts: 6
Can somebody from Unix Shell/Perl scripting guide me at least with the logic?

Thanks in advance.

Pinki
Reply With Quote
  #3  
Old 01-02-2008
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
Quote:
Originally Posted by jamie123 View Post
I have a hash reference with the below entries

IL 100.25
MN 200.25
MO 300
WI 100
IL 200
MN 300
If you insist on saying this is a hash reference in Perl, I will say that is impossible, because a hash does not permit multiple instances of the same key!

You should post the exact form of your structure that carries those data. Code is better than words in programming.

If you have a structure, then doing what you want is easy. A hash is designed to eliminate identical keys so what you can do is similar to:

Code:
@array = (
['IL',     100.25],
['MN',    200.25],
['MO',    300],
['WI',    100],
['IL',     200],
['MN',   300],
);
foreach (@array) {  
    $hash{$_->[0]} += $_->[1];
}
Then you will get what you want in %hash.

By the way, please don't PM me or anyone personally for asking technical questions. Technical questions should belong to this forum and be seen publicly. A PM will not catch my attention earlier if I am not by the computer at all.
Reply With Quote
  #4  
Old 01-03-2008
Registered User
 

Join Date: Jan 2008
Location: Chicago
Posts: 6
Thanks a lot for your response.

I am completely new to perl and am not familiar with the scripting concepts and hence used hash incorrectly.

However, here's the piece of code that writes the data into the associative array or structure (not sure how it should be referrenced).

////////
$data_file="testfile";

open(IN, "$data_file") || die "Cannot open $data_file for reading!";
my $row_num=0;

while (read IN, $data, 138) {

if ( $data != ""){
my $company = substr $data, $record[0]{'start_pos'},$record[0]{'length'};
my $dac = substr $data, $record[1]{'start_pos'},$record[1]{'length'};
my $state = substr $data,$record[2]{'start_pos'},$record[2]{'length'};
my $amt = substr $data,$record[3]{'start_pos'},$record[3]{'length'};
my $type = substr $data, $record[4]{'start_pos'},$record[4]{'length'};
my $proc_date = substr $data, $record[5]{'start_pos'},$record[5]{'length'};
my $src_sys_cd = substr $data, $record[6]{'start_pos'},$record[6]{'length'};
print ("$company ,$dac,$state,$amt,$proc_date,$src_sys_cd \n");
push @final_comp, {comp_stat_dac => $company.$dac.$state, agg_amt => $amt, type =>$type, proc_date =>$proc_date, src_sys_cd=>$src_sys_cd};

}
} close(IN);

//////////

I have to find out the sum of the agg_amt for the same comp_stat_dac.

Say for example, If there are multiple entries for the comp_stat_dac, like IL, MN or ST, I have to sum the agg_amt for every state and store it in a array or hash or print it in a file.

Please help me. I am completely lost.

Thanks,
Jamie
Reply With Quote
  #5  
Old 01-03-2008
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
It appears like the program is trying to extract some information from some fixed size record in a file. @final_comp is an array of hash reference containing data from each record.

If all you want to is to sum rows with the same "comp_stat_dac" field, that is simple:

Code:
foreach (@final_comp) {
    $sum{$_->{'comp_stat_dac'}} += $_->{'agg_amt'};
}
# Output in %sum
use Data::Dumper;
print Dumper(\%sum);
Reply With Quote
  #6  
Old 01-03-2008
Registered User
 

Join Date: Jan 2008
Location: Chicago
Posts: 6
Thanks a lot. You are a genius. That really helped.

However, How do i find out for which state is the amount summed up?

I want the final output also in an array of hashes, something like below


@AoH ={
{comp_stat_dac => "IL",
agg_amt => "500",
},
{
comp_stat_dac => "MN",
agg_amt => "400",
}
{
comp_stat_dac => "MO",
agg_amt => "600",
}
};

Thanks in advance. And please excuse me for such questions.
Reply With Quote
  #7  
Old 01-03-2008
Registered User
 

Join Date: Jan 2008
Location: Chicago
Posts: 6
I am sorry, I didn't see the last lines of code "Dataumper". It did work for me.
Is there any tutorial which explains in detail about the array of hash references & advanced concepts like this.

I really appreciate your timely help. Many thanks to you.

Thanks,
Jamie
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0