in reply to reference question

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}};

Replies are listed 'Best First'.
Re^2: reference question
by convenientstore (Pilgrim) on Jul 29, 2007 at 03:15 UTC
    Something wrong ...
    ~/script/perl/temp@myserver >cat !$ cat ./perl.tty4 #!/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"; } } ~/script/perl/temp@myserver >./!$ ././perl.tty4 Can't use string ("1") as an ARRAY ref while "strict refs" in use at . +/./perl.tty4 line 15. main::(././perl.tty4:13): my $ttys = @{$ttys_of{$user}}; DB<1> main::(././perl.tty4:13): my $ttys = @{$ttys_of{$user}}; DB<1> main::(././perl.tty4:15): if (@$ttys == 1) { DB<1> Can't use string ("1") as an ARRAY ref while "strict refs" in use at . +/./perl.tty4 line 15, <IN> chunk 142. Debugged program terminated. Use q to quit or R to restart, use O inhibit_exit to avoid stopping after program termination, h q, h R or h O to get additional info.
      Sorry about that. That's why one should not play around with more than one version of code before posting their solution. I temporarily thought about dereferencing the array to make it simpler to access in the loop. But I personally would never do this, so I removed the temporary simplification ... almost.

      The code has been updated with the correction.

      - Miller
Re^2: reference question
by convenientstore (Pilgrim) on Jul 29, 2007 at 01:35 UTC
    thank you, I will try your code, I was just following some online tutorial.. I guess they were trying to show the easiest way to code and I wanted to modify its code. but I like your code better..!! will give it a shot. thank you.