Forum: Ruby ruby one-liner needed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 kevin.jackson (Guest)
on 2005-12-02 10:01
(Received via mailing list)
Here's one taht I know is possible (I'm sure I've seen it somewhere
before)

I have 2 arrays and I'd like to change them into a Hash
stuff = ["1", "2", "3"]
Item.column_names.each { |column_name| new_item[column_name] = stuff[?]
}

The problem is how to index within the block to move along the stuff
array, and how to deal with the fact that most of the time the stuff
array is shorter than the column_names array

I'm sure using collect, map of something else ruby-ish that this is more
than possible, but I can't find a solution right now

Kev
896cfc242a7762467c2a0b2af86598e5?d=identicon&s=25 neoneye (Guest)
on 2005-12-02 10:17
(Received via mailing list)
On 12/2/05, Kev Jackson <kevin.jackson@it.fts-vn.com> wrote:
> I have 2 arrays and I'd like to change them into a Hash


pairs=%w(a b c d).zip(%w(x y));Hash[*pairs.flatten]
=> {"a"=>"x", "b"=>"y", "c"=>nil, "d"=>nil}
06c1bab0fb222c7426c02887cd728936?d=identicon&s=25 johannes.friestad (Guest)
on 2005-12-02 10:21
(Received via mailing list)
I'm sure there are many ways to do it, here's one:

a=[3,4,5,6]
b=%w{three four five six seven}
res={}
a.length.times {|i| break if i>=b.length; res[a[i]]=b[i]; }

It handles the situation where either array is shorter than the other.

jf
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 kevin.jackson (Guest)
on 2005-12-02 10:41
(Received via mailing list)
Johannes Friestad wrote:

>
>
>
>
I'm using...

new_item = Hash.new
Item.column_names.each { |column_name| new_item[column_name] = row.shift
}

at the moment and it's working, so I won't replace it with these
suggestions

zip was the method I knew about but couldn't remember :)

Kev
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 w_a_x_man (Guest)
on 2005-12-02 11:06
(Received via mailing list)
Kev Jackson wrote:
> Here's one taht I know is possible (I'm sure I've seen it somewhere before)
>
> I have 2 arrays and I'd like to change them into a Hash
> stuff = ["1", "2", "3"]
> Item.column_names.each { |column_name| new_item[column_name] = stuff[?] }
>
> The problem is how to index within the block to move along the stuff
> array, and how to deal with the fact that most of the time the stuff
> array is shorter than the column_names array

stuff = ["1", "2", "3"]
Item.column_names.each_with_index { |column_name, i|
   new_item[column_name] = stuff[i] }
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 rosco (Guest)
on 2005-12-02 11:26
(Received via mailing list)
On Fri, 02 Dec 2005 09:00:30 -0000, Kev Jackson
<kevin.jackson@it.fts-vn.com> wrote:

>
> I'm sure using collect, map of something else ruby-ish that this is more
> than possible, but I can't find a solution right now
>
> Kev
>
>

Well, theres lots of ways, but one is to inject the Hash as someone here
showed me yesterday:

	items.column_names.inject({}) { |hash, column_name| hash[column_name] =
row.shift ; hash }

(I wanted to eliminate the '; hash' at the end of the closure but
couldn't
find a way yet...)

However, I personally prefer Simon's solution, which can be a real
one-liner too :)

	Hash[*items.column_names.zip(row).flatten]
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-12-02 15:39
(Received via mailing list)
Hi --

On Fri, 2 Dec 2005, Kev Jackson wrote:

> I'm sure using collect, map of something else ruby-ish that this is more than
> possible, but I can't find a solution right now

   a = [1,2,3]
   b = [4,5,6,7,8]
   Hash[*a.zip(b[0,a.size]).flatten
   => {1=>4, 2=>5, 3=>6}

The problem with flatten is that it can be overzealous, but it looks
like you probably don't have nested arrays.  If you do, you can get
the flattenx package from RAA and use flatten_once.


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-12-02 17:08
(Received via mailing list)
Hi --

On Fri, 2 Dec 2005, David A. Black wrote:

>> The problem is how to index within the block to move along the stuff array,
>> and how to deal with the fact that most of the time the stuff array is
>> shorter than the column_names array
>>
>> I'm sure using collect, map of something else ruby-ish that this is more
>> than possible, but I can't find a solution right now
>
>  a = [1,2,3]
>  b = [4,5,6,7,8]
>  Hash[*a.zip(b[0,a.size]).flatten
>  => {1=>4, 2=>5, 3=>6}

Actually just use the shorter one first and zip won't fill it out with
nils:

   Hash[*a.zip(b).flatten]


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-12-02 17:12
(Received via mailing list)
Hi --

On Sat, 3 Dec 2005, David A. Black wrote:

>>>
>>
>>  a = [1,2,3]
>>  b = [4,5,6,7,8]
>>  Hash[*a.zip(b[0,a.size]).flatten
>>  => {1=>4, 2=>5, 3=>6}
>
> Actually just use the shorter one first and zip won't fill it out with
> nils:
>
>  Hash[*a.zip(b).flatten]

I hereby swear I will stop posting until I am being torn in no more
than ten directions at the same time :-)  In other words: I realize
that you can't just switch them around at will.  So you have to decide
whether you're happy to have nils or if you want to truncate the hash
based on available values.

So adapt as needed :-)


David

P.S. One of the things I've been grappling with today is (the third
day of) intermittent/non-existent Internet service -- and a mysterious
reboot on my server.  So I got and read this thread out of order.
Sorry if I reinvented anyone's wheel :-)

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!
This topic is locked and can not be replied to.