in reply to help with 2D arrays with perl requested
I know you've specified a 2D matrix, but in this case, I think a hash would be simpler. Think of the hash as a sparse array. Something like this:
--roboticus#!/usr/bin/perl -w use warnings; use strict; my @animals = ("cat", "dog", "fish", "horse", "pig"); my %count; while (<DATA>) { my ($x,$y) = split /\s/; ($x,$y) = ($y,$x) if ($x gt $y); $count{$x}{$y}++; } for my $x (sort keys %count) { for my $y (sort keys %{$count{$x}}) { print $count{$x}{$y}," people have both a ", $x, " and a ", $y, "\n"; } } __DATA__ cat dog cat fish pig dog horse fish pig fish dog fish fish cat
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: help with 2D arrays with perl requested
by roboticus (Chancellor) on Apr 29, 2006 at 12:53 UTC | |
by ruzam (Curate) on Apr 29, 2006 at 22:51 UTC | |
by roboticus (Chancellor) on Apr 30, 2006 at 14:17 UTC | |
by Anonymous Monk on Apr 29, 2006 at 13:08 UTC | |
by roboticus (Chancellor) on Apr 29, 2006 at 13:19 UTC |