Angharad has asked for the wisdom of the Perl Monks concerning the following question:
and what I need is to create a matrix like thisob1, ob2, 34 ob1, ob3, 56 ob1, ob4, 12 ob1, ob5, 78 ob1, ob6, 23 ob3, ob1, 56 ob7, ob1, 23 ob8, ob1, 12 ob9, ob1, 90 etc ...
And for any 'duplicate results' like theseob1 ob2 ob3 ob4 ob5 ob6 ob7 ob8 ob9 ob1 0 34 56 12 78 23 23 12 90 ob2 34 ob3 56 ob4 12 ob5 78 ob6 23 ob7 23 ob8 12 ob9 90
Only take the first instance (as the result is the same, regardless of the direction).ob1, ob3, 56 ob3, ob1, 56
The results I get using the code above is:#!/usr/local/bin/perl use strict; my $data = $ARGV[0]; # open file here open(DATA, "$data") || die "cant open file for reading\n"; my %table; my %rows; my %cols; for(<DATA>) { my($row,$col,$val) = split ','; $table{$row}{$col} = $val; $rows{$row}++; $cols{$col}++; } for my $col (sort keys %cols) { print "\t$col"; } print "\n"; for my $row (sort keys %rows) { print "$row\t"; for my $col (sort keys %cols) { print $table{$row}{$col} if defined $table{$row}{$col}; print "\t"; } print "\n"; }
Which isn't quite what I need, but I don't know how to fix it (due to my blind spot regarding hashes I suspect).ob1 ob2 ob3 ob4 ob5 ob6 ob1 34 56 12 78 23 ob3 56 ob7 23 ob8 12 ob9 90
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: printing out a matrix for a data list
by rodion (Chaplain) on Aug 05, 2006 at 14:59 UTC | |
|
Re: printing out a matrix for a data list
by lima1 (Curate) on Aug 05, 2006 at 13:50 UTC | |
|
Re: printing out a matrix for a data list
by Tanktalus (Canon) on Aug 05, 2006 at 14:09 UTC | |
|
Re: printing out a matrix for a data list
by gellyfish (Monsignor) on Aug 05, 2006 at 22:46 UTC | |
by Angharad (Pilgrim) on Aug 07, 2006 at 09:09 UTC | |
|
Re: printing out a matrix for a data list
by Angharad (Pilgrim) on Aug 05, 2006 at 15:06 UTC | |
|
Re: printing out a matrix for a data list
by TedPride (Priest) on Aug 06, 2006 at 22:20 UTC | |
by Angharad (Pilgrim) on Aug 07, 2006 at 09:19 UTC |