in reply to Re: Shorten this code
in thread Shorten this code

Let's get rid of the $state variable.
#!/usr/bin/perl -l -w0777 @users{split /\s+/, <DATA>}=(); while(sleep 2) { $_ = `users`; for my $n (keys %users) { if ($users{$n} xor /\b$n\b/) { $users{$n} = /\b$n\b/; print localtime() . " $n has logged ", $users{$n} ? "in" : "out" +; } } }
UPDATE: There are a number of golf tricks that I've left out. For instance ("out", "in")[$users{$n} = /\b$n\b/] manages to both remember and select the current state, removing one line. But readability suffers.

Replies are listed 'Best First'.
Re^3: Shorten this code
by hostyle (Scribe) on Aug 13, 2004 at 17:44 UTC

    Thanks everyone for your contributions. I have only one main question:

    #!/usr/bin/perl -l -w0777

    Whats the 0777 do? And has it anything to do with not using strict?