Re: [ILUG] Re: Need Help with Perl Script

From: Peter Heslin (Peter.Heslin at domain ucd.ie)
Date: Tue 02 May 2000 - 18:46:55 IST


On Tue, May 02, 2000 at 03:45:57PM +0100, Dave O Connor wrote:
> That'd ouput the return value from that nasty shell command, which you don't
> really want. A slightly more efficient (resourcewise, anyway) would be
> something like:
>
> sub num_users
> {
> open(USERS,"/usr/bin/users|");
>
> my $users = <USERS>;
>
> close USERS;
>
> my at domain userlist = split(/\s+/,$users);
>
> my at domain uniqlist;
>
> foreach ( at domain userlist)
> {
> if (!grep($_, at domain uniqlist))
> {
> push( at domain uniqlist,$_);
> }
> }
>
> return scalar at domain uniqlist;
> }

Same idea:

perl -e 'print "There are ", scalar keys %{{map {$_ => 1} split " ", `users`}}, " users logged in.\n"'

Hashes are useful for finding unique elements in a list.

Peter



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:05:59 GMT