in reply to Re^2: trying to decide best data structure for problem at hand.
in thread trying to decide best data structure for problem at hand.

It's there because in your OP you say "... I need to identify on what host each user-id ...". It's not used because I didn't need it to illustrate the main issue. However if you change the final print to:

for my $name (@unlike) { my @hosts = map {$entries{$name}{$_}{host}} keys %{$entries{$name} +}; print "$name found on \n\t", join ("\n\t", @hosts), "\n\n"; }

then the output is:

bjose found on /var/tmp/passwd.hostname2.platform /var/tmp/passwd.hostname1.platform

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^4: trying to decide best data structure for problem at hand.
by mikejones (Scribe) on Jan 05, 2007 at 20:48 UTC
    I don't understand this logic or data structure, but I will attempt to explain it in English.
    for every element in array unlike (which is username) map keys values from hash entries to ??? with name and host. Assign t +his to the array hosts.
    I understand the print statement, but the data structure build I do not.
    Why do you need keys %{$entries{$name} and why not just keys %{$entries}?
    I would like to see output like so or something to that effect: I tried adding values uid and gecos but got errors and played for awhi +le and could not get it printed correctly. So you are using name and uid as the keys and host,gecos and gid as va +lues? I seem to struggle with hashes even though I have read Learning and Pr +ogramming Perl. Any advice you have? wlprdadm found with uids of 134 and 135 on /home/dbsmith/passwd.eipdbmp1.hpux $gecos /home/dbsmith/passwd.carappp1.hpux $gecos as an example. thank you!

      %entry is a HoHoH - Hash of Hash of Hash. The first hash is keyed by name. That accesses a hash keyed by UID. That accesses a hash keyed by various parameters including the host name. In the line:

      my @hosts = map {$entries{$name}{$_}{host}} keys %{$entries{$name}};

      we generate a list of the hosts associated with a particular name. The hosts are in the 'host' parameter in the hash accessed by UID.

      keys %{$entries{$name}} generates a list of UIDs for a given name. Remember that hashes and arrays only store scalar values so what is stored is really a reference to a hash. So the % sigil in %{...} dereferences the hash reference to be a hash which keys then returns the list of keys for.

      For each UID in the list map {$entries{$name}{$_}{host}} retreives the host name - the map generates an output list with each element from the input list replaced by the matching host name.


      DWIM is Perl's answer to Gödel
        Ok thik I am getting this a little more. I printed it using Dumper like so:
        print Dumper(\%entries),"\n"; which produces 'kloesch' => { '291' => { 'gecos' => '', 'host' => '/home/dbsmith/passwd.sp +sdbp.hpux', 'gid' => '102' } }, 'sapdp4' => { '484' => { 'gecos' => '', 'host' => '/home/dbsmith/passwd.dud +bdv02.aix', 'gid' => '208' } },
        I have been banging my head at how to get at gecos and gid and uid in the right printed format. The output I would like it is:
        wlprdadm => uid #s, gid and gecos found on /home/dbsmith/passwd.eipdbmp1.hpux /home/dbsmith/passwd.carappp1.hpux but of course being uid, gid and gecos actual values. Finally, can a hash only contain keys like so: my %hash; my array= (a..d); $hash{$_}++ foreach @array;
        thank you