A hash is a data structure, which assigns a key to value.
In Perl the key is given in the curly braces. A key/value
pair is entered like this: $hash{$key} = $value (assuming
the variables $key and $value, hold the key and value
respectivly).
> open (EXIST, "/tmp/users");
> @exist = <EXIST>;
> close (EXIST);
This read the file /tmp/users into an array. An array elemnt is
one line of the file, including \n!.
>
> %lookup = ();
> @users = ();
>
> foreach $item (@exist) { $lookup{$item} = 1 }
Now you go through the array and enter a username as the key.
The value is not important, so the value is always 1. If the
value is not important, why do we need a hash? The hash enables
us to check very fast, if it contains a certain key. If we had
saved the users in an array, we had to go through the entire array
for each user we want to check.
A problem you may experience is that all users end with \n (see above
comment). I would suggest changing the code to:
foreach $item (@exist) {
chomp $item; #remove trailing newline
$lookup{$item} = 1;
}
>
> foreach $item (@new_users)
> {
now you go through the list with the new users. Maybe you should use
chomp $item here as well. It depends on where you got the data from.
(It can't hurt to use chomp, as it will only remove \n, and nothing
else).
> unless ($lookup{$item})
> {
"unless $lookup{$item}" means "if the user in $item is not already in the
hash $lookup". That's because if the user is known on the system a
key/value pair is included in the hash with key = username and value = 1.
If there isn't such a pair in the hash, the user is not known, and we
add him to an array.
> push(@users, $item);
> }
> }
>
Now @users should contain all users from @new_users which are not in
@exist.
I hope this helps, otherwise ask again.
cr