#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $str = "one 1 two 2 three 3 odd_element";
my %hash = split / /, $str;
print Dumper \%hash;
####
Odd number of elements in hash assignment at test.pl line 7.
$VAR1 = {
'three' => '3',
'one' => '1',
'odd_element' => undef,
'two' => '2'
};
####
#!/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;
####
$VAR1 = {
'odd_element' => undef,
'one' => '1',
'two' => '2',
'three' => '3'
};