in reply to Re: Which Data Structure Should I Be Using?
in thread Which Data Structure Should I Be Using?

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.

Replies are listed 'Best First'.
Re3: Which Data Structure Should I Be Using?
by dragonchild (Archbishop) on Apr 02, 2002 at 20:36 UTC
    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.