Help for this page

Select Code to Download


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