#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    52
    Rep Power
    3

    How to create a Hash of Arrays?


    So i've decided to take fishmongers advice and create a HOA but i'm not sure how. Essentially what my script is doing now is selecting the group name and setting it to words [0] and the members to words [3]. I want to be able to call my HAO and foreach group check if each of members match and if not state which member in the group doesn't match. Right now if I was to key AD and print it I would get all the names but now what group they are associated with , it won't let me reference to a specific group or a specific member in that group.
    PHP Code:
    foreach my $group (keys %ff) {

        @
    adfinalcmd = `adquery group $group`;

    foreach 
    my $user (@adfinalcmd) {

        @
    words split /\:/, $user;
           
    $agroups $words [0];
        
    my @userlist sort (split /,/,$words [3]);
        
    my $alist "@userlist";
        
        
    $AD {$alist} = $agroups;
        
        
        
    }

     } 
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Do you want a hash of users, or a hash of groups?

    i.e., do you want:
    $AD{user} = [ @groups ];

    or:
    $AD{group} = [ @users ];

    Depending o the type of comparisons you need, it might be better to use a HoH (Hash of Hashes) instead of a HoA.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    52
    Rep Power
    3
    Originally Posted by FishMonger
    Do you want a hash of users, or a hash of groups?

    i.e., do you want:
    $AD{user} = [ @groups ];

    or:
    $AD{group} = [ @users ];

    Depending o the type of comparisons you need, it might be better to use a HoH (Hash of Hashes) instead of a HoA.
    This whole entire hash thing is new to me. I'm thinking i want a hash of users. What i'm planning on doing is the list of AD groups names and NIS groups match. The users don't. So foreach NIS group I want to compare each individal user to the coressponding AD groups users and see if I get a match. If not report this "$user doesnt exist in AD group $groupname";
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    52
    Rep Power
    3
    Originally Posted by hcrosex3
    This whole entire hash thing is new to me. I'm thinking i want a hash of users. What i'm planning on doing is the list of AD groups names and NIS groups match. The users don't. So foreach NIS group I want to compare each individal user to the coressponding AD groups users and see if I get a match. If not report this "$user doesnt exist in AD group $groupname";
    Though you are right maybe a hash of hashes would be better because currently i have this logic to make sure that the groups alone match hash of hashes might make this cleaner.

    The variables were just test made up variables not what I actually plan on using. :P
    PHP Code:
    print " Comparison of  Groups \n";

    # Compare groups to AD list

    my %ff;
    foreach 
    my $groupname (keys %ADgroups) {

        if (
    exists $n_group {$groupname}){
        
        
    $groupname next;

        }else { 
        
        
    my $test $groupname;
        
    #print "$test \n";
            
    $ff {$groupname} = $test;
            

    }
    }


    #Compare groups to NIS list
    my %gg;
    foreach 
    my $groupname (keys %groups) {

        if (
    exists $a_group {$groupname}){
        
        
    $groupname next;

        }else { 
        
    my $test $groupname;
        
    $gg {$groupname} = $test;
        
            
            

    }
    }


    #comparing ff list to gg list

    foreach my $comp (keys %ff) {
        if (
    exists $gg {$comp}){
        
    next;
        }else {
        
     print 
    "$comp does not match gg \n";
     
    next;

    }

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    52
    Rep Power
    3
    Originally Posted by FishMonger
    Do you want a hash of users, or a hash of groups?

    i.e., do you want:
    $AD{user} = [ @groups ];

    or:
    $AD{group} = [ @users ];

    Depending o the type of comparisons you need, it might be better to use a HoH (Hash of Hashes) instead of a HoA.
    *bumps*
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    52
    Rep Power
    3
    I added this logic but of course it's not working because the user ids are not in alphabetical order


    PHP Code:
    my %hash2 reverse %ADgroups
    my %hash3 reverse %groups
    my @present_only_in_hash1 grep exists $hash2{$_}, values %groups
    my @present_only_in_hash2 grep exists $hash3{$_}, values %ADgroups
    my @common grep exists $hash3{$_}, values %ADgroups

    print 
    "Present only in HASH 1 = @present_only_in_hash1\n"
    print 
    "Present only in HASH 2 = @present_only_in_hash2\n"
    print 
    "COMMON values = @common\n"
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    I am not sure of what you are trying to do, but it is quite easy to sort your hash either by your keys or by your values.

    By keys:

    Code:
    my @keys = sort (keys %hash);
    or by values

    Code:
    my @values = reverse sort {$hash{a} cmp $hash{b} } keys %hash

IMN logo majestic logo threadwatch logo seochat tools logo