develooper Front page | perl.macperl | Postings from August 2005

Re: [MacPerl] how to grow a hash of hashes]

From:
Detlef Lindenthal
Date:
August 1, 2005 08:22
Subject:
Re: [MacPerl] how to grow a hash of hashes]
Message ID:
42EE3DBD.5210AE3@lindenthal.com
  ##  Louis ecrire:
  ##  > It would be interesting to assess each scheme
  ##  > in terms of resource requirements
  ##  > (running time and memory).

  ##  You are write, and hashes should not
  ##  be bigger than "medium size"   ;-)
  ##  at least MacPerl hashes.


  ###########################################
  ##  You can put one complete URL into one hash record:

 print "\n\n=======  Test per pedes:  ======\n";
$H{'64.207.97.183'}{ng}{gov}{nigeria}{www}{is_read}=1;
#$H{'64.207.97.183'}{ng}{gov}{nigeria}{is_read}=1;
  ## The following might be slimmer?
 $H{'64.207.97.183'}{"nigeria.gov.ng"}{is_read}=1;
 $H{'64.207.97.183'}{"www.nigeria.gov.ng"}{is_read}=1;

  ##  Then you can test:

 print     "nigeria.gov.ng\n" if exists $H{'64.207.97.183'}{ng}{gov}{nigeria}{is_read};
 print "www.nigeria.gov.ng\n" if exists $H{'64.207.97.183'}{ng}{gov}{nigeria}{www}{is_read};
 print "WWW.NIGERIA.GOV.NG\n" if exists $H{'64.207.97.183'}{"www.nigeria.gov.ng"}{is_read};
 print     "NIGERIA.GOV.NG\n" if exists $H{'64.207.97.183'}{"nigeria.gov.ng"}{is_read};

  ##  Or test with this:
print "\n\n=======  Test with showHash:  ======\n";
showHash(\%H);


sub showHash {
  $indent++;
  my %H = %{$H=shift};
  for $Hk(keys %H)  {
     $Hv = $H{$Hk};
     if (ref($Hv))  {
       print "--"x($indent-1), "$Hk\n" ;
       showHash($Hv);        }
     else   {
       print ".."x($indent-1), "$Hk => " . $H{$Hk} ."\n" ;
     } }
 $indent--;
 }

  ##  Probably the choice what hash scheme to use
  ##  will depend on what kind of tests you wish to
  ##  have afterwards.

  ##  Au revoir,
  ##  Detlef



=This will output:


=======  Test per pedes:  ======
www.nigeria.gov.ng
WWW.NIGERIA.GOV.NG
NIGERIA.GOV.NG

=======  Test with showHash:  ======
64.207.97.183
--ng
----gov
------nigeria
--------www
- - - - - is_read => 1
--www.nigeria.gov.ng
- - is_read => 1
--nigeria.gov.ng
- - is_read => 1









nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About