in reply to How to split a non even number of string elements into a hash [RESOLVED]

Hello thanos1983,

How about this?

use strict; use warnings; use Data::Dumper; my $string = "one 1 two 2 three 3 odd_element"; my @elements = split / /, $string; push @elements, undef if @elements % 2; my %hash = @elements; print Dumper \%hash;

Output:

22:41 >perl 1750_SoPW.pl $VAR1 = { 'three' => '3', 'odd_element' => undef, 'two' => '2', 'one' => '1' }; 22:42 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: How to split a non even number of string elements into a hash
by choroba (Cardinal) on Feb 09, 2017 at 14:53 UTC
    Or even
    my @elements = split / /, $string; my %hash = (@elements, (undef) x (@elements % 2));

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^2: How to split a non even number of string elements into a hash
by thanos1983 (Parson) on Feb 09, 2017 at 14:17 UTC

    Hello Athanasius

    That is exactly what I was not able to come up with. Sort simple easy to follow.

    Thanks again for your time and effort.

    Seeking for Perl wisdom...on the process of learning...not there...yet!