Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting list to table

2 views
Skip to first unread message

jay

unread,
Nov 5, 2009, 9:36:19 AM11/5/09
to
Hi All,

Am new to using newgroup for help, but have exhausted my searching
online for a solution

I have a long list of data of associations between values with a value
to that association as follows..

(var) to (var) = (var) hits
A B 1
B A 1
A B 3
B A 3
A C 7
C A 2

And need to build a table as follows that accumulates the above:

row is (from)
column is (to)

A B C
A 0 4 7
B 4 0 0
C 2 0 0

Just can't seam to figure out how to manage this programatically.

Any help or guidance much appreciated !!!


Cheers,


Jay

Loki Harfagr

unread,
Nov 6, 2009, 11:26:40 AM11/6/09
to
Thu, 05 Nov 2009 06:36:19 -0800, jay did cat :

Hope this wil help though not in perl, I guess the port should be
quite straightforward (note the OFS means Output Field Separator and
ORS stands for Output Record Separator)

Given the sample file:
---------------
$ cat MISCFILES/var2varHits
### (var) to (var) (hits)
### wants a result matrix as
### row is (from)
### col is (to)
### content is (sigmahits)
###


A B 1
B A 1
A B 3
B A 3
A C 7
C A 2

---------------

the 'almost meta' code here:
---------------
awk '
/^[^#]/{
froms[$1]
tos[$2]
hits[$1","$2]+=$3
}
END{
printf OFS OFS
for(t in tos){
printf t OFS
}
printf ORS
for(f in froms){
printf f OFS
for(t in tos){
printf 0+hits[f","t] OFS
}
printf ORS
}
}' MISCFILES/var2varHits
---------------

will produce:
---------------


A B C
A 0 4 7
B 4 0 0
C 2 0 0

---------------

Loki Harfagr

unread,
Nov 6, 2009, 11:49:08 AM11/6/09
to
Fri, 06 Nov 2009 16:26:40 +0000, Loki Harfagr did cat :

> Thu, 05 Nov 2009 06:36:19 -0800, jay did cat :
>
>> Hi All,
>>
>> Am new to using newgroup for help, but have exhausted my searching
>> online for a solution

ooops, I didn't see your post in c.l.a. before and why didi you
multipost instead of crosspost!? Ah sorry...

George Mpouras

unread,
Nov 13, 2009, 6:27:13 AM11/13/09
to
Hello Jay,

The code bellow will do what you want. I hope it helps a litle

George Mpouras


while (<DATA>) { (@_) = split /\s+/; $hash{$_[0],$_[1]} += $_[2] }

print $hash{A,C};

__DATA__

0 new messages