my $str = "one 1 two 2 three 3 odd_element"; my $cnt = () = $str =~ /(\s)/g; # count spaces my %hash = split /\s/, $str, $cnt + 1; # split on spaces + 1