#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash; my $str = "one 1 two 2 three 3 odd_element"; my @array = split / /, $str; while (@array) { my $key = shift @array; my $value = shift @array; if (defined $key && defined $value) { $hash{$key} = $value; } else { $hash{$key} = undef; } } print Dumper \%hash;