CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Summing up Values (A HASH?)

 



remi
Novice

Jul 28, 2003, 5:14 AM

Post #1 of 4 (447 views)
Summing up Values (A HASH?) Can't Post

Hello to you all again,
I have a little prob. I have a tab file with several lines. Some of these lines have to be summes up since they have some common values, like this:

Code
Journal Archives	129.233.21.50	0	2	0	2	FHG IGB Stuttgart 
Journal Archives 129.233.22.163 0 10 0 10 FHG IGB Stuttgart
Journal Archives 129.233.22.167 0 2 0 2 FHG IGB Stuttgart
Journal Archives 129.233.22.234 0 1 0 1 Uni Bremen
Journal Archives 129.233.22.251 0 2 0 2 Uni Bremen
Journal Archives 130.133.202.88 0 3 0 3 FU Berlin
Journal Archives 130.133.222.203 0 22 0 22 FU Berlin
Journal Archives 130.133.46.144 0 2 0 2 FU Berlin
Journal Archives 130.149.10.169 0 4 0 4 TU Berlin
Journal Archives 130.149.101.73 0 2 0 2 TU Berlin
Journal Archives 130.149.104.106 0 1 0 1 TU Berlin
Chemical Research 134.100.204.51 1 2 0 3 Uni Hamburg
Chemical Research 134.100.205.124 0 2 0 2 Uni Hamburg
Chemical Research 134.100.205.152 0 0 1 1 TU Berlin
Chemical Research 134.100.205.169 0 1 0 1 TU Berlin
Chemical Research 134.100.209.104 0 0 1 1 TU Berlin
Organometallics 160.45.27.155 0 1 0 1 Uni Hamburg
Organometallics 160.45.27.156 4 8 0 12 Uni Hamburg
Organometallics 192.108.70.1 0 16 0 16 Uni Hamburg
Organometallics 192.108.70.50 4 186 4 194 Uni Bremen
Organometallics 194.95.63.241 0 6 0 6 Uni Bremen
Organometallics 195.37.184.165 0 1 1 2 Uni Bremen

The first and the last columns of each lines have to be compared. If they are the same with those in the coming lines, the values (columns 3,4,5 & 6 ) of these similler lines have to be summed up. Similarity is considered to be that columns 1 and 7 are the same.
I do not know if HASH can do this or has anyone a good idea of doing this?

Thanks in Advance.
Remi


(This post was edited by remi on Jul 28, 2003, 5:28 AM)


davorg
Thaumaturge / Moderator

Jul 29, 2003, 1:18 AM

Post #2 of 4 (439 views)
Re: [remi] Summing up Values (A HASH?) [In reply to] Can't Post

Hashes and autovivification are your friend.

Code
 
#!/usr/local/bin/perl

use strict;
use warnings;

my %data;

while (<DATA>) {
chomp;
my @row = split /\t/;

foreach my $col (2 .. 5) {
$data{$row[0]}{$row[6]}[$col - 2] = $row[$col];
}
}

foreach my $src (keys %data) {
foreach my $org (keys %{$data{$src}}) {
print "$src / $org: @{$data{$src}{$org}}\n";
}
}

__DATA__
Journal Archives 129.233.21.50 0 2 0 2 FHG IGB Stuttgart
Journal Archives 129.233.22.163 0 10 0 10 FHG IGB Stuttgart
Journal Archives 129.233.22.167 0 2 0 2 FHG IGB Stuttgart
Journal Archives 129.233.22.234 0 1 0 1 Uni Bremen
Journal Archives 129.233.22.251 0 2 0 2 Uni Bremen
Journal Archives 130.133.202.88 0 3 0 3 FU Berlin
Journal Archives 130.133.222.203 0 22 0 22 FU Berlin
Journal Archives 130.133.46.144 0 2 0 2 FU Berlin
Journal Archives 130.149.10.169 0 4 0 4 TU Berlin
Journal Archives 130.149.101.73 0 2 0 2 TU Berlin
Journal Archives 130.149.104.106 0 1 0 1 TU Berlin
Chemical Research 134.100.204.51 1 2 0 3 Uni Hamburg
Chemical Research 134.100.205.124 0 2 0 2 Uni Hamburg
Chemical Research 134.100.205.152 0 0 1 1 TU Berlin
Chemical Research 134.100.205.169 0 1 0 1 TU Berlin
Chemical Research 134.100.209.104 0 0 1 1 TU Berlin
Organometallics 160.45.27.155 0 1 0 1 Uni Hamburg
Organometallics 160.45.27.156 4 8 0 12 Uni Hamburg
Organometallics 192.108.70.1 0 16 0 16 Uni Hamburg
Organometallics 192.108.70.50 4 186 4 194 Uni Bremen
Organometallics 194.95.63.241 0 6 0 6 Uni Bremen
Organometallics 195.37.184.165 0 1 1 2 Uni Bremen


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


remi
Novice

Jul 29, 2003, 1:51 AM

Post #3 of 4 (438 views)
Re: [davorg] Summing up Values (A HASH?) [In reply to] Can't Post

Thanks,
it is working well. I needed this because Database takes minutes. I had to add a plus to this line to give what i wanted:

Code
$data{$row[0]}{$row[6]}[$col - 2] += $row[$col];

Thanks again


davorg
Thaumaturge / Moderator

Jul 29, 2003, 1:59 AM

Post #4 of 4 (437 views)
Re: [remi] Summing up Values (A HASH?) [In reply to] Can't Post

Yeah, that's the deliberate mistake I added to ensure that you understood my solution :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives