Hello fasoli,
Yes, it’s easy to tweak the code to generate the desired output format:
#! perl use strict; use warnings; use constant SORT_BY_X => 1; my (%data, %y_values); while (<DATA>) { my ($x, $y) = split; $x = sprintf "%.2f", $x; $y = sprintf "%.2f", $y; push @{ $data{$x} }, $y; ++$y_values{$y}; } my @x = sort { $a <=> $b } keys %data; my @y = sort { $a <=> $b } keys %y_values; print "--------------\n"; print " X Y Z\n"; print "--------------\n"; if (SORT_BY_X) { for my $x (@x) { tally($x, $_) for @y; } } else # sort by y { for my $y (@y) { tally($_, $y) for @x; } } print "--------------\n"; sub tally { my ($x, $y) = @_; my $count = 0; for (@{ $data{$x} }) { ++$count if $_ == $y; } printf "%5.2f %5.2f %2d\n", $x, $y, $count; } __DATA__ 0.1234567890 1.2345678901 52.2456789012 17.2345678901 0.1234567890 1.2345678901 22.3456789012 3.4567890123 22.35 1.234
Output:
14:12 >perl 1635_SoPW.pl -------------- X Y Z -------------- 0.12 1.23 2 0.12 3.46 0 0.12 17.23 0 22.35 1.23 1 22.35 3.46 1 22.35 17.23 0 52.25 1.23 0 52.25 3.46 0 52.25 17.23 1 -------------- 14:12 >perl 1635_SoPW.pl
(But next time you ask the monks for help like this, please show the code you’ve tried, and explain where and how it failed to do what you expected.)
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^3: convert columns into matrix and get population count?
by Athanasius
in thread convert columns into matrix and get population count?
by fasoli
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |