[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Most simple way to do "array + array => hash"

Hal E. Fulton

3/31/2006 5:40:00 AM

Meino Christian Cramer wrote:
> Hi,
>
> Is there a ruby-like way to make an hash from two equeal sized
> arrays, one containing the keys only and the other the values ?
>
> Thanks a lot for any help in advance !
>

Would this work?

Hash[*arr1.zip(arr2)]


Hal



9 Answers

Meino Christian Cramer

3/31/2006 5:55:00 AM

0

Kelly Norton

3/31/2006 6:02:00 AM

0

Hal Fulton wrote:
> Meino Christian Cramer wrote:
>> Hi,
>>
>> Is there a ruby-like way to make an hash from two equeal sized
>> arrays, one containing the keys only and the other the values ?
>>
>> Thanks a lot for any help in advance !
>>
>
> Would this work?
>
> Hash[*arr1.zip(arr2)]
>
>
> Hal
>

I think a flatten is in needed as well:

Hash[*keys.zip(vals).flatten]

/kel


Hal E. Fulton

3/31/2006 6:04:00 AM

0

Meino Christian Cramer wrote:
> From: Hal Fulton <hal9000@hypermetrics.com>
> Subject: Re: Most simple way to do "array + array => hash"
> Date: Fri, 31 Mar 2006 14:39:58 +0900
>
> Hi Hal,
>
> no, it seems not (from my irb):

Oops, forgot to flatten.

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

irb(main):004:0> Hash[*a.zip(b).flatten]
=> {"V"=>"V", "K"=>"U", "W"=>"S", "L"=>"K", "A"=>"Z", "X"=>"P", "M"=>"R", "B"=>"X",
"Y"=>"Q", "N"=>"A", "C"=>"M", "Z"=>"O", "O"=>"B", "D"=>"I", "P"=>"H", "E"=>"D",
"Q"=>"E", "F"=>"G", "R"=>"W", "G"=>"F", "S"=>"N", "H"=>"L", "T"=>"Y", "I"=>"T",
"U"=>"C", "J"=>"J"}


Cheers,
Hal



rickhg12hs

3/31/2006 7:35:00 AM

0

> "little things" like converting a "A" to a 65

Here's another way:

$ ruby -w -e 'puts ?A'
65

Dave Baldwin

3/31/2006 9:08:00 AM

0


On 31 Mar 2006, at 08:38, rickhg12hs wrote:

>> "little things" like converting a "A" to a 65
>
> Here's another way:
>
> $ ruby -w -e 'puts ?A'
> 65

I have always used 'A'[0] to return the ASCII value of A and as
rickhg12hs wrote ?A will do the same thing. I cannot find any
reference to this in Pickaxe so can anyone explain how this works?

Thanks,

Dave.



Pit Capitain

3/31/2006 11:18:00 AM

0

Dave Baldwin schrieb:
> I have always used 'A'[0] to return the ASCII value of A and as
> rickhg12hs wrote ?A will do the same thing. I cannot find any
> reference to this in Pickaxe so can anyone explain how this works?

Page 50 (first edition) or here:

http://phrogz.net/ProgrammingRuby/tut_stdtypes.ht...

Regards,
Pit


coachhilton

3/31/2006 10:24:00 PM

0

Very cute. I hopped onto irb to check out what each step of
Hash[*keys.zip(vals).flatten] does (I'd not used zip nor flatten in
some time) and I can't quite figure out what function asterisk, ie,
*keys performs (leaving it out or executing *keys.zip(vals).flatten
results in a ruby error and I don't see an explanation of this use of *
in the nutshell book.) Can one of you enlighten me?

Thx!

Ken

matthew.moss.coder

3/31/2006 10:41:00 PM

0

> def foo(a, b, c)
> "a:#{a} b:#{b} c:#{c}"
> end
=> nil

> foo(1, 2, 3)
=> "a:1 b:2 c:3"

> x = [4, 5, 6]
=> [4, 5, 6]

> foo(x)
ArgumentError: wrong number of arguments (1 for 3)

> foo(*x)
=> "a:4 b:5 c:6"


On 3/31/06, coachhilton@gmail.com <coachhilton@gmail.com> wrote:
> Very cute. I hopped onto irb to check out what each step of
> Hash[*keys.zip(vals).flatten] does (I'd not used zip nor flatten in
> some time) and I can't quite figure out what function asterisk, ie,
> *keys performs (leaving it out or executing *keys.zip(vals).flatten
> results in a ruby error and I don't see an explanation of this use of *
> in the nutshell book.) Can one of you enlighten me?


Logan Capaldo

3/31/2006 11:28:00 PM

0


On Mar 31, 2006, at 5:28 PM, coachhilton@gmail.com wrote:

> I can't quite figure out what function asterisk, ie,
> *keys performs (leaving it out or executing *keys.zip(vals).flatten
> results in a ruby error and I don't see an explanation of this use
> of *
> in the nutshell book.) Can one of you enlighten me?

* is the unary unarray operator

basically what it does delete the square brackets:

f(*[1,2,3]) -> f(1,2,3)