in reply to trying to decide best data structure for problem at hand.

I'd key by name then uid:

use strict; use warnings; my %entries; my $key; my $host; while (<DATA>) { chomp; next if ! length; if (m|^/|) { $host = $_; } elsif (/(\w+)\s+(\d+)\s+(\d+)\s+(.*)/) { my ($name, $uid, $gid, $gecos) = ($1, $2, $3, $4); $entries{$name}{$uid} = { # User data keyed by uid gid => $gid, gecos => $gecos, host => $host, }; } else { print "Can't parse >$_<\n"; } } my @unlike = grep {keys (%{$entries{$_}}) > 1} keys %entries; print "@unlike"; __DATA__ /var/tmp/passwd.hostname1.platform nguyenhe 1929 20 Henry Nguyen,555-555-555 bjose 1990 20 Bobby Jose,x3338 /var/tmp/passwd.hostname2.platform vjain 2098 20 Vineet Kumar Jain, offshore llai 2122 20 Levius Lai bjose 1995 20 Bobby Jose,x3338

Prints:

bjose

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: trying to decide best data structure for problem at hand.
by mikejones (Scribe) on Jan 03, 2007 at 21:50 UTC
    wow cannot believe I did not see how simple it really was. Anyway thank you, but in your code you do not use
    $entries{$name}{$uid} = { # User data keyed by uid gid => $gid, gecos => $gecos, host => $host,
    gid => $gid, gecos => $gecos, host => $host ???

      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
        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!