in reply to Re^2: Perl and arrays of arrays?
in thread Perl and arrays of arrays?

(Was your reply misplaced?)

Your comment is wrong, but your code works:

# USAGE: $masterhash{$ipaddress} = @aliases
That's the same as:
$masterhash{$ipaddress} = scalar @aliases
What you want in this case is:
$masterhash{$ipaddress} = \@aliases
The only justification for array of hashes I can come up with is if the number of entries is very large, and you manage duplicate hosts. (For example, the file is already sorted by host, and you keep track of the last host. If the current host is the same, the new data is added to the last host entry. Otherwise, a new host entry is created first.) But searching the host array to find the correct entry on a large array is problematic.

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^4: Perl and arrays of arrays?
by Koolstylez (Scribe) on Aug 17, 2005 at 19:51 UTC
    Oops, I should have replied to the original post.

    The comment was meant to be an indicator of the usage, I wasn't worried about making it syntactically correct.

    I personally like the hash of arrays approach, as it more closely mirrors the way I think about the problem: a key (IP address) that can have one or more aliases. However, as I noted in my original post, it is not the most efficient way to do things...just a bit easier to understand for a beginner IMHO.
      I personally like the hash of arrays approach, as it more closely mirrors the way I think about the problem: a key (IP address) that can have one or more aliases. However, as I noted in my original post, it is not the most efficient way to do things...just a bit easier to understand for a beginner IMHO.
      I understand what you're saying, but I'd go in a different direction.

      If a beginner understands AoA:

      $array[$row][$col] = $pixel;
      and that hashes are sort of arrays with different brackets:
      $array{$row}{$col} = $pixel;
      then it's a small leap to suggest that (here, at least) the keys are the interesting bits, and the values don't matter:
      $hosts{$ip}{$alias} = 1;

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        Yeah, when you put it that way, I see what you mean. Not to mention the fact that the HoH method is far more scalable because you don't have to explicitly filter duplicates. TMTOWDI :)