![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Can somebody from Unix Shell/Perl scripting guide me at least with the logic?
Thanks in advance. Pinki |
|
#3
|
|||
|
|||
|
Quote:
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];
}
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. |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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);
|
|
#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. |
|
#7
|
|||
|
|||
|
I am sorry, I didn't see the last lines of code "Data
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 |
|||
| Google The UNIX and Linux Forums |