[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Multidimensional hash - get all not blank keys

Michael ..

2/15/2009 11:45:00 AM

Hello.

I have this Hash.

result={"1"=>{"6"=>"",
"7"=>"",
"8"=>"",
"1"=>"Value1",
"2"=>"",
"3"=>"Value2",
"4"=>"",
"5"=>""},
"2"=>{"6"=>"",
"7"=>"",
"8"=>"Value3",
"1"=>"",
"2"=>"",
"3"=>"",
"4"=>"",
"5"=>""},
"3"=>{"6"=>"",
"7"=>"",
"8"=>"",
"1"=>"",
"2"=>"",
"3"=>"",
"4"=>"",
"5"=>""},
"4"=>{"6"=>"",
"7"=>"",
"8"=>"Value4",
"1"=>"",
"2"=>"Value5",
"3"=>"",
"4"=>"",
"5"=>""}
}


Is it possible to get all keys with a the value.
1,[1 => Value1]
1,[3 => Value2]
2,[8 => Value3]
4,[8 => Value4]
4,[2 => Value5]

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

12 Answers

Andrew Timberlake

2/15/2009 12:12:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Sun, Feb 15, 2009 at 1:44 PM, Michael .. <xinu@gmx.at> wrote:

> Hello.
>
> I have this Hash.
>
> result={"1"=>{"6"=>"",
> "7"=>"",
> "8"=>"",
> "1"=>"Value1",
> "2"=>"",
> "3"=>"Value2",
> "4"=>"",
> "5"=>""},
> "2"=>{"6"=>"",
> "7"=>"",
> "8"=>"Value3",
> "1"=>"",
> "2"=>"",
> "3"=>"",
> "4"=>"",
> "5"=>""},
> "3"=>{"6"=>"",
> "7"=>"",
> "8"=>"",
> "1"=>"",
> "2"=>"",
> "3"=>"",
> "4"=>"",
> "5"=>""},
> "4"=>{"6"=>"",
> "7"=>"",
> "8"=>"Value4",
> "1"=>"",
> "2"=>"Value5",
> "3"=>"",
> "4"=>"",
> "5"=>""}
> }
>
>
> Is it possible to get all keys with a the value.
> 1,[1 => Value1]
> 1,[3 => Value2]
> 2,[8 => Value3]
> 4,[8 => Value4]
> 4,[2 => Value5]
>
> thanks
> michael
> --
> Posted via http://www.ruby-....
>

=> result.each do |k,v| puts "#{k} = #{v.reject{|k2,v2| v2 == ''}.inspect}"
end
1 = {"1"=>"Value1", "3"=>"Value2"}
2 = {"8"=>"Value3"}
3 = {}
4 = {"8"=>"Value4", "2"=>"Value5"}

You should be able to take that and massage it into what you need.

Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

"I have never let my schooling interfere with my education" - Mark Twain

pjb

2/15/2009 12:17:00 PM

0

"Michael .." <xinu@gmx.at> writes:
> I have this Hash.
>
> result={"1"=>{"6"=>"",
> "7"=>"",
> "8"=>"",
> "1"=>"Value1",
> "2"=>"",
> "3"=>"Value2",
> [...]
> Is it possible to get all keys with a the value.

Yes, it is possible.




Oh! Perhaps you meant "How?"? Well perhaps you don't see them because
they're behind your eyes, but you've got a few billion interconnected
neurons inside your skull. They're called a "brain". So normally,
you should be able to switch it "on", start to think, and it should
help you to program a function to "get all keys with a the value".
Try it, you'll be pleased!

--
__Pascal Bourguignon__

David A. Black

2/15/2009 12:27:00 PM

0

On Sun, 15 Feb 2009, Pascal J. Bourguignon wrote:

> "Michael .." <xinu@gmx.at> writes:
>> I have this Hash.
>>
>> result={"1"=>{"6"=>"",
>> "7"=>"",
>> "8"=>"",
>> "1"=>"Value1",
>> "2"=>"",
>> "3"=>"Value2",
>> [...]
>> Is it possible to get all keys with a the value.
>
> Yes, it is possible.
>
>
>
>
> Oh! Perhaps you meant "How?"? Well perhaps you don't see them because
> they're behind your eyes, but you've got a few billion interconnected
> neurons inside your skull. They're called a "brain". So normally,
> you should be able to switch it "on", start to think, and it should
> help you to program a function to "get all keys with a the value".
> Try it, you'll be pleased!

Please don't post messages like this to this list. There's no benefit
to anyone.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

pjb

2/15/2009 12:37:00 PM

0

"David A. Black" <dblack@rubypal.com> writes:

> On Sun, 15 Feb 2009, Pascal J. Bourguignon wrote:
>
>> "Michael .." <xinu@gmx.at> writes:
>>> I have this Hash.
>>>
>>> result={"1"=>{"6"=>"",
>>> "7"=>"",
>>> "8"=>"",
>>> "1"=>"Value1",
>>> "2"=>"",
>>> "3"=>"Value2",
>>> [...]
>>> Is it possible to get all keys with a the value.
>>
>> Yes, it is possible.
>>
>>
>>
>>
>> Oh! Perhaps you meant "How?"? Well perhaps you don't see them because
>> they're behind your eyes, but you've got a few billion interconnected
>> neurons inside your skull. They're called a "brain". So normally,
>> you should be able to switch it "on", start to think, and it should
>> help you to program a function to "get all keys with a the value".
>> Try it, you'll be pleased!
>
> Please don't post messages like this to this list. There's no benefit
> to anyone.

Hopefully, it should be of benefit to the OP. He should really start
to think by himself, and start to learn how to find the information he
lacks on the web. There is the whole Ruby language reference
available on the web, there is ri, as soon as he knows what a hash
table is, he should be able to activate a couple of neurons and find
out how to do what he wanted to do.


--
__Pascal Bourguignon__

David A. Black

2/15/2009 12:47:00 PM

0

On Sun, 15 Feb 2009, Pascal J. Bourguignon wrote:

> "David A. Black" <dblack@rubypal.com> writes:
>
>> On Sun, 15 Feb 2009, Pascal J. Bourguignon wrote:
>>
>>> "Michael .." <xinu@gmx.at> writes:
>>>> I have this Hash.
>>>>
>>>> result={"1"=>{"6"=>"",
>>>> "7"=>"",
>>>> "8"=>"",
>>>> "1"=>"Value1",
>>>> "2"=>"",
>>>> "3"=>"Value2",
>>>> [...]
>>>> Is it possible to get all keys with a the value.
>>>
>>> Yes, it is possible.
>>>
>>>
>>>
>>>
>>> Oh! Perhaps you meant "How?"? Well perhaps you don't see them because
>>> they're behind your eyes, but you've got a few billion interconnected
>>> neurons inside your skull. They're called a "brain". So normally,
>>> you should be able to switch it "on", start to think, and it should
>>> help you to program a function to "get all keys with a the value".
>>> Try it, you'll be pleased!
>>
>> Please don't post messages like this to this list. There's no benefit
>> to anyone.
>
> Hopefully, it should be of benefit to the OP.

It won't be, and can't be. There's no information in it and no advice,
just a string of snide, smug insults. Please keep a civil tone on this
list. Thanks.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

pjb

2/15/2009 2:55:00 PM

0

"David A. Black" <dblack@rubypal.com> writes:

> On Sun, 15 Feb 2009, Pascal J. Bourguignon wrote:
>
>> "David A. Black" <dblack@rubypal.com> writes:
>>
>>> On Sun, 15 Feb 2009, Pascal J. Bourguignon wrote:
>>>
>>>> "Michael .." <xinu@gmx.at> writes:
>>>>> I have this Hash.
>>>>>
>>>>> result={"1"=>{"6"=>"",
>>>>> "7"=>"",
>>>>> "8"=>"",
>>>>> "1"=>"Value1",
>>>>> "2"=>"",
>>>>> "3"=>"Value2",
>>>>> [...]
>>>>> Is it possible to get all keys with a the value.
>>>>
>>>> Yes, it is possible.
>>>>
>>>>
>>>>
>>>>
>>>> Oh! Perhaps you meant "How?"? Well perhaps you don't see them because
>>>> they're behind your eyes, but you've got a few billion interconnected
>>>> neurons inside your skull. They're called a "brain". So normally,
>>>> you should be able to switch it "on", start to think, and it should
>>>> help you to program a function to "get all keys with a the value".
>>>> Try it, you'll be pleased!
>>>
>>> Please don't post messages like this to this list. There's no benefit
>>> to anyone.
>>
>> Hopefully, it should be of benefit to the OP.
>
> It won't be, and can't be. There's no information in it and no advice,
> just a string of snide, smug insults. Please keep a civil tone on this
> list. Thanks.

You are much more insulting than I. You are assuming he doesn't know
how to do it, how to search and program the solution by himself.

I'm only saying he's lazy.


--
__Pascal Bourguignon__

Julian Leviston

2/15/2009 3:01:00 PM

0

I agree. People have a tendency to not try. They come wanting answers
without hving thought about it themselves.

Blog: http://random8.ze...
Learn rails: http://sensei.ze...

On 16/02/2009, at 1:53 AM, pjb@informatimago.com (Pascal J.
Bourguignon) wrote:

> "David A. Black" <dblack@rubypal.com> writes:
>
>> On Sun, 15 Feb 2009, Pascal J. Bourguignon wrote:
>>
>>> "David A. Black" <dblack@rubypal.com> writes:
>>>
>>>> On Sun, 15 Feb 2009, Pascal J. Bourguignon wrote:
>>>>
>>>>> "Michael .." <xinu@gmx.at> writes:
>>>>>> I have this Hash.
>>>>>>
>>>>>> result={"1"=>{"6"=>"",
>>>>>> "7"=>"",
>>>>>> "8"=>"",
>>>>>> "1"=>"Value1",
>>>>>> "2"=>"",
>>>>>> "3"=>"Value2",
>>>>>> [...]
>>>>>> Is it possible to get all keys with a the value.
>>>>>
>>>>> Yes, it is possible.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Oh! Perhaps you meant "How?"? Well perhaps you don't see them
>>>>> because
>>>>> they're behind your eyes, but you've got a few billion
>>>>> interconnected
>>>>> neurons inside your skull. They're called a "brain". So
>>>>> normally,
>>>>> you should be able to switch it "on", start to think, and it
>>>>> should
>>>>> help you to program a function to "get all keys with a the value".
>>>>> Try it, you'll be pleased!
>>>>
>>>> Please don't post messages like this to this list. There's no
>>>> benefit
>>>> to anyone.
>>>
>>> Hopefully, it should be of benefit to the OP.
>>
>> It won't be, and can't be. There's no information in it and no
>> advice,
>> just a string of snide, smug insults. Please keep a civil tone on
>> this
>> list. Thanks.
>
> You are much more insulting than I. You are assuming he doesn't know
> how to do it, how to search and program the solution by himself.
>
> I'm only saying he's lazy.
>
>
> --
> __Pascal Bourguignon__
>

Tim Hunter

2/15/2009 3:34:00 PM

0

Julian Leviston wrote:
> I agree. People have a tendency to not try. They come wanting answers
> without hving thought about it themselves.

I agree as well. However, I think it's possible to be helpful short of
simply supplying the answer.

I'm speaking as a long-time jerk who struggles every day to not be
sarcastic and mean in public forums. I know, intellectually at least,
that it's never appropriate to be rude or to judge somebody based on a
single post. I don't always succeed but I try.

The ideal answer to a question like the O.P.'s is helpful, positive, and
correct. For example, answers like "Consider using the <this method>
in <this class>." Or write a prose description of an algorithm. Point to
information sources. "You can use ri to get more information about..."
Or "Here's a blog post that might help."

If I can't be helpful, positive, and give a correct answer, I keep my
hands off the keyboard and wait for somebody else who can.

--
RMagick: http://rmagick.ruby...

William James

2/15/2009 8:28:00 PM

0

David A. Black wrote:

> Please don't post messages like this to this list. There's no benefit
> to anyone.

Please don't make him feel unwelcome. I think it's interesting to see
a Lisp-expert using Ruby.

William James

2/15/2009 8:44:00 PM

0

Michael .. wrote:

> Hello.
>
> I have this Hash.
>
> result={"1"=>{"6"=>"",
> "7"=>"",
> "8"=>"",
> "1"=>"Value1",
> "2"=>"",
> "3"=>"Value2",
> "4"=>"",
> "5"=>""},
> "2"=>{"6"=>"",
> "7"=>"",
> "8"=>"Value3",
> "1"=>"",
> "2"=>"",
> "3"=>"",
> "4"=>"",
> "5"=>""},
> "3"=>{"6"=>"",
> "7"=>"",
> "8"=>"",
> "1"=>"",
> "2"=>"",
> "3"=>"",
> "4"=>"",
> "5"=>""},
> "4"=>{"6"=>"",
> "7"=>"",
> "8"=>"Value4",
> "1"=>"",
> "2"=>"Value5",
> "3"=>"",
> "4"=>"",
> "5"=>""}
> }
>
>
> Is it possible to get all keys with a the value.
> 1,[1 => Value1]
> 1,[3 => Value2]
> 2,[8 => Value3]
> 4,[8 => Value4]
> 4,[2 => Value5]
>

result.delete_if{|k,v| {} == v.delete_if{|a,b| b==""}}
p result

{"1"=>{"1"=>"Value1", "3"=>"Value2"}, "2"=>{"8"=>"Value3"},
"4"=>{"8"=>"Value4", "2"=>"Value5"}}