Joost and others have already pointed out the mistake you made with references.
However, I'd also like to point out that it would help if you simplified your code. Instead of keeping separate structures for count and the list of ttys, why not just keep those in a single hash of arrays structure.
Also, please note that you should limit the scope of your variables to their smallest possible scope. This gives "use strict;" the best chance of helping with syntax errors and also is the most obvious way to self-document your code.
#!/usr/bin/perl -w
use strict;
my %ttys_of;
foreach (`who`) {
my ($user, $tty) = /(\S+)\s+(\S+)/;
push @{$ttys_of{$user}}, $tty;
}
foreach my $user (sort keys %ttys_of) {
my $ttys = $ttys_of{$user};
if (@$ttys == 1) {
print "user $user is ONLY logged in at one place $ttys->[0]\n"
+;
} else {
print "user $user is logged in at: " . join(' ', sort @$ttys)
+. "\n";
}
}
- Miller
Update 4:20 UTC: Fixed this line
my $ttys = @{$ttys_of{$user}};
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.