[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Find display value in array

Jon Stenqvist

7/10/2007 12:11:00 PM

Hi,

I have a array that looks like this
Type = [["key","value"],["key..","value.."],["..",".."]]

Now i want to the the display (value) when i have the key, is this a OK
way to do it? Or is there a better way?

Type.select{|key,disp| key == "H" }.collect{|key,value|value}.to_s

--
Posted via http://www.ruby-....

5 Answers

James Gray

7/10/2007 12:20:00 PM

0

On Jul 10, 2007, at 7:10 AM, Jon Stenqvist wrote:

> Hi,
>
> I have a array that looks like this
> Type = [["key","value"],["key..","value.."],["..",".."]]
>
> Now i want to the the display (value) when i have the key, is this
> a OK
> way to do it? Or is there a better way?

Yes. You want the assoc() method:

>> assoc_array = [%w[key1 value1], %w[key2 value2], %w[key3 value3]]
=> [["key1", "value1"], ["key2", "value2"], ["key3", "value3"]]
>> assoc_array.assoc("key2").last
=> "value2"

Hope that helps.

James Edward Gray II

Harry Kakueki

7/10/2007 12:44:00 PM

0

> Type = [["key","value"],["key..","value.."],["..",".."]]
>
> Now i want to the the display (value) when i have the key, is this a OK
> way to do it? Or is there a better way?
>
> Type.select{|key,disp| key == "H" }.collect{|key,value|value}.to_s
>
Is this what you want to do?
If so, I think iteration is easier than that.
I think it is also faster but I did not check.

arr = [["A",2],["H",3],["H",4]]
arr.each {|x| print x[1] if x[0] == "H"}

Harry


--
A Look into Japanese Ruby List in English
http://www.ka...

Jon Stenqvist

7/10/2007 12:55:00 PM

0

Thanks for a good solution!

Now i saw that i had may key on in the last position in the inner array,
is there someother function that i use insted, so i can search for last
item? and return the first.

James Gray wrote:
> On Jul 10, 2007, at 7:10 AM, Jon Stenqvist wrote:
>
>> Hi,
>>
>> I have a array that looks like this
>> Type = [["key","value"],["key..","value.."],["..",".."]]
>>
>> Now i want to the the display (value) when i have the key, is this
>> a OK
>> way to do it? Or is there a better way?
>
> Yes. You want the assoc() method:
>
> >> assoc_array = [%w[key1 value1], %w[key2 value2], %w[key3 value3]]
> => [["key1", "value1"], ["key2", "value2"], ["key3", "value3"]]
> >> assoc_array.assoc("key2").last
> => "value2"
>
> Hope that helps.
>
> James Edward Gray II


--
Posted via http://www.ruby-....

Florian Groß

7/10/2007 1:01:00 PM

0


On Jul 10, 2:54 pm, Jon Stenqvist <j...@equipe.nu> wrote:
> Now i saw that i had may key on in the last position in the inner array,
> is there someother function that i use insted, so i can search for last
> item? and return the first.

Try rassoc. :)


Jon Stenqvist

7/10/2007 2:38:00 PM

0

Aha, sweet ;) Thank you!

Florian Gross wrote:
> On Jul 10, 2:54 pm, Jon Stenqvist <j...@equipe.nu> wrote:
>> Now i saw that i had may key on in the last position in the inner array,
>> is there someother function that i use insted, so i can search for last
>> item? and return the first.
>
> Try rassoc. :)


--
Posted via http://www.ruby-....