my $str = `who`; #### foreach my $line (split /\n/,$str) { my ($user, $remainder) = split /\s/,$_,2; $hash{$user}++; # just increment the value } #### open(PIPE,"who |") or die "Can't run 'who': $!\n"; while() { my ($user, $remainder) = split; # implicit split splits $_ on /\s+/ $hash{$user}++; #### # sort by user foreach my $user(sort keys %hash) { print "$user logged in $hash{$user} times.\n"; } # sort by times users are logged in foreach my $user(sort {$hash{$a} <=> $hash{$b}} keys %hash) { print "$user logged in $hash{$user} times.\n"; }