Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Convert multi dimentional array to array of hashes

Reply
Thread Tools

Convert multi dimentional array to array of hashes

 
 
All4miller All4miller
Guest
Posts: n/a
 
      04-07-2010
Hi,

How can I turn and array like this this

[[1,2,3,4,5],[1,2,3,4,5]]

into this

[{:lt=>1,:ln=>1},{:lt=>2,:ln=>2},{:lt=>3,:ln=>3},{: lt=>4,:ln=>4},{:lt=>5,:ln=>5}]

Cheers
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
AMILIN Aurélien
Guest
Posts: n/a
 
      04-07-2010
You can try something like that :

a = [[1,2,3,4,5],[1,2,3,4,5]]
a.transpose.collect {|e| {:lt => e.first, :ln => e.last} }

Aurélien AMILIN


Le 07/04/2010 11:24, All4miller All4miller a écrit :
> Hi,
>
> How can I turn and array like this this
>
> [[1,2,3,4,5],[1,2,3,4,5]]
>
> into this
>
> [{:lt=>1,:ln=>1},{:lt=>2,:ln=>2},{:lt=>3,:ln=>3},{: lt=>4,:ln=>4},{:lt=>5,:ln=>5}]
>
> Cheers
>


 
Reply With Quote
 
 
 
 
all4 miller
Guest
Posts: n/a
 
      04-07-2010
thanks that worked great
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Const Multi-Dimentional Array Member Variable NvrBst C++ 3 11-13-2008 09:02 PM
multi-dimentional array Joan Gu Ruby 5 03-27-2008 11:45 PM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
How can I know how many elements are there in multi-dimentional array? Abby C Programming 2 09-16-2003 02:51 AM



Advertisments