in reply to Which Data Structure Should I Be Using?

@{$data{$ip}}{@field_names} = [$fields];
That line is an error. You're saying that a slice of a hash reference is set equal to an array reference (whose only element is an array reference, I might add). Better would be:
@{$data{$ip}}{@field_names} = @$fields;
Now, you still have a problem with
my $fields = [@cname, @ports, @banners];
You're saying that you want to create an array reference, pointing to an array of elements from the three arrays in order. I suspect you really want:
my @fields = (\@cname, \@ports, \@banners);
Thus, the complete code would be:
my @fields = (\@cname, \@ports, \@banners); @{$data{$ip}}{@field_names} = @fields;
That should do what you want. :-)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re: Which Data Structure Should I Be Using?
by dru145 (Friar) on Apr 02, 2002 at 20:03 UTC
    Thanks Dragonchild, I'm happy to know that I was closer then I thought. I'm trying to print out each value with this:
    foreach my $ip (keys %data) { print "IP = $ip\n"; for my $cname ( @{$data{$ip}{cname}} ) { print "CName = $cname\n"; } }
    and here is what I get:
    IP = 192.168.30.136 CNAME = salamander.acme.com CNAME = AMSPWD030WKG CNAME = VLW4S CNAME = WKBX0010B-A IP = 192.168.30.130 CNAME = salamander.acme.com CNAME = AMSPWD030WKG CNAME = VLW4S CNAME = WKBX0010B-A IP = 192.168.30.131 CNAME = salamander.acme.com CNAME = AMSPWD030WKG CNAME = VLW4S CNAME = WKBX0010B-A IP = 192.168.30.133 CNAME = salamander.acme.com CNAME = AMSPWD030WKG CNAME = VLW4S CNAME = WKBX0010B-A
    How do I keep the cname value with the ip?

    Thanks,
    Dru
    Another satisfied monk.
      You are "Suffering from Scoping". (As well as two rather nasty side-effects of references, but those'll get fixed.)

      Let's diagram what you're doing with @cname. You've declared it at the top of your program. This implies that you are attempting to gather together a list of computer names from across your entire file. This doesn't sound like what you want to do.

      Instead, you want to gather a list of the computer names for a given IP address. Again, you're almost there.

      push @{$data{$ip}{cname}}, $1 if /\b$ip\b\s{2,}(\w[^\s]*|\[Unknown\])\s{2}/; push @{$data{$ip}{ports}}, $1 if /((\d{1,5}?)\s{2}(\w[^\n]*))\s{2}/; push @{$data{$ip}{banners}}, $1 if /\Q|___\E\s+([^"]*)\.{1,3}\s+\n/;
      This way, you're doing the pushing onto the array you actually want. (Obviously, you remove the declaration of @cname and his friends, cause it's not needed anymore. You also remove the hash-slicing cause it's also not needed anymore.)

      For your personal edification, what was happening was that you were creating one array - @cname. Every instance of your data was referencing this specific array. Every time you modified it, each instance would know about the modifications. Every time you de-referenced it, you were de-referencing the same array.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.