Date: 11 Jul 2001 07:44:56 -0700 From: garry_short@hotmail.com (Garry) Subject: Re: Data structures question. Message-Id: Michael Carman wrote in message news:<3B4B28B3.61950B7C@home.com>... > Garry wrote: > > > > Yes, see the perlfaq4 entry "How do I sort an array by (anything)?" > Thanks for that! I've had a read through it, and tried something similar with strange problems (can't remember what, at the moment). I think I'll have to have a play for a while to get to grips with it. > > Assuming that's not possible, has anyone got any suggestions on > > implementing the following data structure : > > > > array of (unique SRC_IP, > > array of (unique SRC_PORT, > > array of (unique DEST_IP, > > array of (unique DEST_PORT, > > array of (unique PROTOCOL, > > array of (unique DATE, > > array of (unique TIME, > > array of (TYPE, CODE) > > ) > > ) > > ) > > ) > > ) > > ) > > ) > > Hrm. I'm not sure I understand. That pseudocode looks like you want a > eight-dimensional array! (Each SRC_IP would have multiple SRC_PORTs, > each SRC_PORT multiple DEST_IPs, etc...) I don't think you really want > that. :) > Mmm, possibly - I was thinking more along the lines of a tree structure, but I understand what you mean :-) > I'd suggest using a LoH (list of hashes). Something like this: > > #!/usr/bin/perl -w > use strict; > > my @connection; > > while () { > next if /^#/; > chomp; > my ($src_ip, $src_port, $dest_ip, $dest_port) = split; > > my %link = ( > SRC_IP => $src_ip, > SRC_PORT => $src_port, > DEST_IP => $dest_ip, > DEST_PORT => $dest_port, > ); > push @connection, \%link; > } > > foreach my $n (0 .. $#connection) { > my $link = $connection[$n]; > print "$n:\n"; > foreach my $k (sort keys %$link) { > print "\t$k - $link->{$k}\n"; > } > } > > __DATA__ > # src_ip src_port dest_ip dest_port > 1.1.1.1 80 2.1.1.1 80 > 1.1.1.2 10 2.1.1.2 34 > 1.1.1.3 40 2.1.1.3 12 > > -mjc Now *that* looks fantastic - thanks! I'd looked at hashes, but couldn't find any examples that were close enough for me to see how to apply them. There's enough info here for me to get to grips with it now. Oh, one point for clarification ... the line foreach my $n (0 .. $#connection) { ... does that work along similar lines as ... for ($n = 0; $n <= $#connection; $n++) { ? Thanks a lot, Garry