RE: named hash inside of hash

2004-06-08 Thread Arms, Mike
Look up "autovivification":

 
http://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=perl+autovivification
&btnG=Search

Just part of the power of Perl.

--
Mike Arms


> -Original Message-
> From: Matt Bazan [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, June 08, 2004 4:51 PM
> To: [EMAIL PROTECTED]
> Subject: RE: named hash inside of hash
> 
> 
> Thanks for help, it's starting to make sense.  I don't understand this
> line:
> 
>  $ips{$source_ip}->{$dest_ip}++;
> 
> how can you access and increment something that doesnt exist?  Thanks
> again.
> 
> > -Original Message-
> > From: Peter Guzis [mailto:[EMAIL PROTECTED] 
> > Sent: Tuesday, June 08, 2004 12:50 PM
> > To: Matt Bazan; Thomas, Mark - BLS CTR
> > Cc: [EMAIL PROTECTED]
> > Subject: RE: named hash inside of hash
> > 
> > Like this?
> > 
> > ## begin code
> > 
> > use strict;
> > use Data::Dumper;
> > 
> > my %ips;
> > 
> > foreach my $source_ip ('192.168.1.2', '192.168.1.1', 
> '192.168.1.12') {
> > 
> >   foreach my $dest_ip ('192.168.1.3', '192.168.9.19', 
> '192.168.9.1') {
> > 
> > $ips{$source_ip}->{$dest_ip}++;
> > 
> >   }
> > 
> > }
> > 
> > print Dumper \%ips;
> > 
> > ## end code
> > 
> >  
> > Peter Guzis
> > Web Administrator, Sr.
> > ENCAD, Inc.
> > - A Kodak Company
> > email: [EMAIL PROTECTED]
> > www.encad.com 
> > 
> > -Original Message-
> > From: Matt Bazan [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, June 08, 2004 12:36 PM
> > To: Thomas, Mark - BLS CTR
> > Cc: [EMAIL PROTECTED]
> > Subject: RE: named hash inside of hash
> > 
> > 
> > Ok..here's a diagram:
> > 
> > I need to setup a data structure that will keep track of a 
> > source IP address.  Each source IP address I then need to 
> > link to a destination IP
> > (note: there can be multiple destiantion IP addresses for 
> > each source) and a destination IP counter.  So, something like:
> > 
> > source IP 1 -> dst ip 1, counter
> >dst ip 2, counter
> >dst ip 3, counter
> > source IP 2 -> dst ip 1, counter
> >dst ip 2, counter
> > 
> > etc, etc.  Thanks! 
> > 
> > > -Original Message-
> > > From: Thomas, Mark - BLS CTR [mailto:[EMAIL PROTECTED]
> > > Sent: Tuesday, June 08, 2004 12:28 PM
> > > To: Matt Bazan
> > > Cc: [EMAIL PROTECTED]
> > > Subject: RE: named hash inside of hash
> > > 
> > > > create a hash who's first key's value is an anon ref to a
> > > hash who's
> > > > second key's value is a non-anon hash..get my drift?  
> Is there an 
> > > > easier way to do this?  Thanks..
> > > 
> > > I have no idea what you're saying... so here's a stab in the dark:
> > > 
> > > my $data = {
> > >  this => {
> > >is =>   {
> > >  nested => 'true'
> > >},
> > >isnt => {
> > >  nested => 'false'
> > >}
> > >  }
> > >};
> > > 
> > > print $data->{this}->{is}->{nested}; #prints 'true'
> > > print $data->{this}->{isnt}->{nested}; #prints 'false'
> > > 
> > > Does that help?
> > > 
> > > 'perldoc perldsc' may help too.
> > > 
> > > 
> > > 
> > > -- 
> > > Mark Thomas[EMAIL PROTECTED] 
> > > Internet Systems Architect DigitalNet, Inc. 
> > > 
> > > $_=q;KvtuyboopuifeyQQfeemyibdlfee;; y.e.s. ;y+B-x+A-w+s; ;y;y; 
> > > ;;print;;
> > >   
> > > 
> > > 
> > > 
> > 
> > ___
> > Perl-Win32-Users mailing list
> > [EMAIL PROTECTED]
> > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> > 
> > 
> 
> ___
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: named hash inside of hash

2004-06-08 Thread Andy_Bach
> I don't understand this line:

 $ips{$source_ip}->{$dest_ip}++;

> how can you access and increment something that doesnt exist?

Fancy name: autovivification - perl, w/ your best interests at heart 
(generally) knows what you want to do, so it automagically creates the 
source_ip hash entry (if it doesn't exist), creates the anon-hash (ditto), 
adds a dest_ip key (ibid) and sees the incr. op, so set a value to zero 
and increments it - bingo
 $ips{$source_ip}->{$dest_ip}++;
 if ( $ips{$source_ip}->{$dest_ip} == 1 ) {
 print "First time $source_ip has sent $dest_ip packets!\n";
 }

On the other hand, autoV works for increment, but you should see a warning 
(you are using -w/strict right?) if you try::
 if ( $ips{$source_ip}->{$dest_ip} == 0 ) {
 print "First time $source_ip has sent $dest_ip packets!\n";
 }
 $ips{$source_ip}->{$dest_ip}++;

Here, if it *is* the first time, as dest_ip doesn't exist, you'll see:
Use of uninitialized value in numeric eq (==) at 

In this case:
 unless ( $ips{$source_ip}->{$dest_ip} ) {
 print "First time $source_ip has sent $dest_ip packets!\n";
 }
 $ips{$source_ip}->{$dest_ip}++;

gets around that as there's no compare - basically you're checking for 
truth, existence or non-zero.

Make any sense?

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

Hardware, n.:
The parts of a computer system that can be kicked.

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs