Marza has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks, I am writing a script that will search our domain and get cpu infomation via the registry. After completing it, the manager wanted the 9x info as well. There is no clean way to get that info via the registry so I decided to hard code the info in a hash of hash of lists. Reason is that there are a few domains and he wanted the info for each domain. I am trying to call it in this fashion:

my %win9x = ( domain1 => { Test98 => [ "Windows 98", , 1, 233, 128], TestME => [ "Windows Me", , 1, 233, 64], Win981 => [ "Windows 98SR2", , 1, 600, 512], Win982 => [ "Windows 98SR2", , 1, 600, 512], WinME1 => [ "Windows Me", 1, , 600, 512], WinME2 => [ "Windows Me", 1, , 600, 512] } ); unless ($domain = shift) { @ARGV = Win32::DomainName or die "Unable to obtain the domain +name\n"; } foreach $domain (@ARGV) { print "\nDOMAIN:\t$domain\n\n"; AddWin9x{%win9x}; }

The sub routine will not work because it reports. Can't locate object method "AddWin9x" via package "DOMAIN1" (perhaps you forgot to load "DOMAIN1"?)

sub AddWin9x { my %hoh = shift or next;; my $computer; my $os; my $cpu; my $mhz; my $ram; my $sp; print "\n\nWindows 98//Me\n\n"; foreach {$hoh{$domain{$computer}}) { ($os, $sp, $cpu, $mhz, $ram) = $computer; printf ("%20s %3s %-15s %3s %4s %5s\n",$computer, $os, + $cpu, $mhz, $ram); } }

The sub I have not tested out because I have not gotten that far. Sorry but my head hurts from banging it on the desk! Thanks for any suggestions!

Replies are listed 'Best First'.
Re: Trying to pass a hash of hash of lists
by tstock (Curate) on Mar 14, 2002 at 01:59 UTC
    • AddWin9x(%win9x); # not AddWin9x{%win9x};
    • you probably want to pass the hash by reference though
      AddWin9x(\%win9x); # then extract the values inside the sub
    • on the last foreach you have { instead of ( also
    • next inside a sub seems like bad form to me


    Tiago

      Well that was dumb! Thanks! The second foreach was a typo for this post... Droped out the next. It bothered me everytime I looked at it and your comment motivated me. Have a good one!

Re: Trying to pass a hash of hash of lists
by Juerd (Abbot) on Mar 14, 2002 at 08:26 UTC

    There is no clean way to get that info via the registry so I decided to hard code the info in a hash of hash of lists.

    I don't like spreading bad news, but a hash of lists is not really possible. A list of lists is neither.

    Lists and arrays are different things. An array is mutable, but lists are not (lists are immutable ("read-only")). Consider:

    # A list (1, 2, 3); # An array assignment (syntax: ARRAY = LIST) my @array = (1, 2, 3); # An array @array # The third element of the array $array[2] # The third element of a list (this actually is a slice) (1, 2, 3)[2] # Arrays are mutable, so the following is possible $array[2]++ # Lists are immutable, so the following is impossible (1, 2, 3)[2]++ # A reference to a named array \@array # A reference to an anonymous array [ 1, 2, 3 ] # A reference to a named array that later becomes anonymous my $ref; { my @array = (1, 2, 3); $ref = \@array; } # A list of references \(1, 2, 3) # Equal to \(1, 2, 3) (\1, \2, \3) # A list of arrays ([1, 2, 3], [4, 5, 6], [7, 8, 9]) # A reference to an (anonymous) array of (anonymous) arrays [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    This is the point where most people think "But what about perllol? That talks about lists of lists!". Well, perllol is wrong, sorry.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      IMO, an array is a list. A list may not be an array. An array is not just a list; it is a special type of list that has extra features. So "list of lists" is just fine, if more vague than necessary since any perl "list of lists" would more precisely be called a "list of references to arrays". But perllorta.pod probably isn't a better name.

      I suppose you could define "list" such that it does not include "array". But that would be a very bad idea, IMO, and would conflict with a lot of the standard Perl documentation.

              - tye (but my friends call me "Tye")

        IMO, an array is a list.

        An array, when used in list context, evaluates to its elements. When used in scalar context, it evaluates to its number of elements. (You'll also notice that there is no "array context", just as there is no "hash context".) Consider:

        # A list (1, 2, 1, 2, 3); # An array @array; # Array assignment (ARRAY = LIST) @array = (1, 2, 1, 2, 3); # List in list context print (1, 2, 1, 2, 3); # 12123 # Array in list context print @array; # 12123 # List in scalar context print scalar (1, 2, 1, 2, 3); # 3 (not 5!!) # Array in scalar context print scalar @array; # 5 (not 3!!)

        So in list context, an array evaluates to the same list that was used in its assignment (unless altered, of course), but in scalar context, a list evaluates to its last element and an array evaluates to its number of elements.

        • You can change an array, you cannot change a list.
        • An array knows its number of elements (MAX in the AV struct, internally), but a list does not.
        • You can reference an array, but you cannot create a reference to a list (instead, a list of scalar references is made).
        • An array is a data type suited for storage, a list is not.
        </code>

        U28geW91IGNhbiBhbGwgcm90MTMgY
        W5kIHBhY2soKS4gQnV0IGRvIHlvdS
        ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
        geW91IHNlZSBpdD8gIC0tIEp1ZXJk