in reply to Re^2: help with 2D arrays with perl requested
in thread help with 2D arrays with perl requested

Wouldn't this accomplish the task more simply?
#!/usr/bin/perl -w use warnings; use strict; my %count; while (<DATA>) { chomp; my ($x,$y) = split /\s/; ($x,$y) = ($y,$x) if ($x gt $y); $count{"$x $y"}++; } foreach (sort keys %count) { print "people with $_: $count{$_}\n"; } __DATA__ cat dog cat fish pig dog horse fish pig fish dog fish fish cat

Replies are listed 'Best First'.
Re^4: help with 2D arrays with perl requested
by roboticus (Chancellor) on Apr 30, 2006 at 14:17 UTC
    ruzam:

    Thanks! That tip will certainly come in handy. Collapsing both keys into one certainly saves a bit of effort.

    Major Update: Someone downvoted this, and I suspect that it's because they thought I was sarcastic or because I didn't mention *why* I thought it would save effort. So I'll elaborate a little: In my array version, I had to use those "next if !defined" bits to prevent error messages from printing. By collapsing both keys into one, all those error checks disappear from the code. So the net improvement is: We remove an inner loop (for *each* use!) and we remove the "next" statements (again, for *each* use).

    Expecting another downvote at any time now..... 8^)

    --roboticus