use Data::Dumper; # to let us print out the results at the end my $var = "xxx:12345 yyy:54321 zzz:13245"; my %hash = $var =~ /(\S+):(\d+)/g; # a pattern match in array context returns a list of the matches # and hash is a list in which odd-numbered items are keys and even- # numbered items are values print Dumper(\%hash);