$ perl -MData::Dumper -e ' > my $str = q{1 human 2 flower 3 fruits 5 human 6 car 9 flower asdgjhashdnh gsdh sjs klja}; > my %hash = > map { ( split m{\s+} )[ 0, 1 ] } > split m{(?x) (?<=\w) \s+ (?=\d)}, $str; > print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ hash } ] );' $hash = { '6' => 'car', '1' => 'human', '3' => 'fruits', '9' => 'flower', '2' => 'flower', '5' => 'human' }; $