in reply to Re: help with 2D arrays with perl requested
in thread help with 2D arrays with perl requested
And now for the code....
--roboticus#!/usr/bin/perl -w use warnings; use strict; my @animals = ("cat", "dog", "fish", "horse", "pig"); # Hash to map an animal to an array index. I'm sure there's a # better way, but I don't know it... my %a2i; my $i=0; for my $a (@animals) { $a2i{$a}=$i++; } my @count; while (<DATA>) { my ($x,$y) = split /\s/; ($x,$y) = ($y,$x) if ($x gt $y); $count[$a2i{$x}][$a2i{$y}]++; } for my $x (sort keys %a2i) { next if !defined $count[$a2i{$x}]; for my $y (sort keys %a2i) { next if !defined $count[$a2i{$x}][$a2i{$y}]; print $count[$a2i{$x}][$a2i{$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^3: help with 2D arrays with perl requested
by ruzam (Curate) on Apr 29, 2006 at 22:51 UTC | |
by roboticus (Chancellor) on Apr 30, 2006 at 14:17 UTC | |
|
Re^3: help with 2D arrays with perl requested
by Anonymous Monk on Apr 29, 2006 at 13:08 UTC | |
by roboticus (Chancellor) on Apr 29, 2006 at 13:19 UTC |