Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to split a non even number of string elements into a hash

by Athanasius (Archbishop)
on Feb 09, 2017 at 12:45 UTC ( [id://1181518]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1181518]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-19 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found