foreach my $line (split /\n/,$str) { my ($user, $remainder) = split /\s/,$_,2; $hash{$user}++; # just increment the value }