Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: making a text file to an hash array

by suhailck (Friar)
on Jul 14, 2010 at 11:00 UTC ( [id://849493]=note: print w/replies, xml ) Need Help??


in reply to making a text file to an hash array

perl -MData::Dumper -e '$_=q[a 2 b 4,5,6 c d e 45,657,-67];@arr=split +/\s+/;do { $arr[$_]!~/\d+/ and splice(@arr,$_,0,[]) } for grep {($_%2 +)} 0 .. $#arr;(%hash)=@arr;print Dumper(\%hash)'

$VAR1 = { 'e' => '45,657,-67', 'c' => [], 'a' => '2', 'b' => '4,5,6', 'd' => [] };


UPDATE:included arrayref in splice for missing values

~suhail

Replies are listed 'Best First'.
Re^2: making a text file to an hash array
by rovf (Priest) on Jul 14, 2010 at 14:06 UTC

    ... only that the OP wanted to have a reference to an empty array for missing values, not an empty string; so we would should transfer the array by map { length ? $_ : [] } @arr at the end.

    -- 
    Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found