# In search of elegant indices searching

**URL:** <https://www.ruby-forum.com/t/in-search-of-elegant-indices-searching/94843>\
**Category:** Ruby\
**Created:** [May 16, 2006, 11:23am UTC](https://www.ruby-forum.com/t/in-search-of-elegant-indices-searching/94843 "2006-05-16T11:23:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 16, 2006, 11:23am UTC](https://www.ruby-forum.com/t/in-search-of-elegant-indices-searching/94843/1 "2006-05-16T11:23:35Z")

</div>

Dear all,

I have a Hash, with equal-length Arrays as values, something like

my\_hash={‘x’,[1,1,3,1,5,6],‘w’,[1,2,3,1,4,5],‘y’,[1,1,1,4,4,6],‘z’,[0,1,1,2,3,  
4]} .

Some of the values in the Arrays occur several times.  
Now, I am looking for an elegant way to extract, for all the values  
of specified keys, all the indices  
where one or more variables do not change their values, i.e., something  
like

class Hash  
def fixed\_vars(which\_vars)

```
     # some elegant code
    return result
end

```

end

res=my\_hash.fixed\_vars([‘x’,‘w’])

giving

res={{‘x’=\>1,‘w’=\>1}=\>[0,3],{‘x’=\>3,‘w’=\>3}=\>[2],{‘x’=\>5,‘w’=\>4}=\>[4],{‘x’=\>6,  
‘w’=\>5}=\>[5]}.

How could that be done?

Thank you!

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 16, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/in-search-of-elegant-indices-searching/94843/2 "2006-05-16T17:17:00Z")

</div>

2006/5/16, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> where one or more variables do not change their values, i.e., something  
> res=my\_hash.fixed\_vars([‘x’,‘w’])
> 
> giving
> 
> res={{‘x’=\>1,‘w’=\>1}=\>[0,3],{‘x’=\>3,‘w’=\>3}=\>[2],{‘x’=\>5,‘w’=\>4}=\>[4],{‘x’=\>6,  
> ‘w’=\>5}=\>[5]}.

That’s a really wired output. Can you explain what that means? I have  
a hard time detecting the pattern here.

robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 16, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/in-search-of-elegant-indices-searching/94843/3 "2006-05-16T19:46:06Z")

</div>

removed\_email\_address@domain.invalid å??é?ï¼?

> Dear all,
> 
> I have a Hash, with equal-length Arrays as values, something like
> 
> my\_hash={‘x’,[1,1,3,1,5,6],‘w’,[1,2,3,1,4,5],‘y’,[1,1,1,4,4,6],‘z’,[0,1,1,2,3,  
> 4]} .
> 
> Some of the values in the Arrays occur several times.  
> Now, I am looking for an elegant way to extract, for all the values  
> of specified keys, all the indices  
> where one or more variables do not change their values, i.e., something  
> like
> 
> class Hash  
> def fixed\_vars(which\_vars)
> 
> ```
> # some elegant code
> return result
> end
> 
> ```
> 
> end
> 
> res=my\_hash.fixed\_vars([‘x’,‘w’])
> 
> giving
> 
> res={{‘x’=\>1,‘w’=\>1}=\>[0,3],{‘x’=\>3,‘w’=\>3}=\>[2],{‘x’=\>5,‘w’=\>4}=\>[4],{‘x’=\>6,  
> ‘w’=\>5}=\>[5]}.

Indeed, I really don’t fully understand what you want , and the code  
below is hacked out and is definitively not elegant. But it works, and  
gives what you want .

module MyHash  
def fixed\_vars(key1, key2)  
res = {}  
def res.  
self.each do |akey, aval|  
if akey == key  
return aval  
end  
end  
nil  
end  
a1 = self[key1]  
a2 = self[key2]  
len = a1.length  
recorded = []  
len.times do |i|  
if a1[i] == a2[i]  
h = {key1=\>a1[i],key2=\>a2[i]}  
res[h]=[] if res[h] == nil  
res[h] \<\< i  
recorded \<\< a1[i]  
end  
end  
len.times do |i|  
if a1[i] != a2[i] and  
(not recorded.include?(a1[i])) and  
(not recorded.include?(a2[i]))  
res[{key1=\>a1[i],key2=\>a2[i]}] = [] if  
res[{key1=\>a1[i],key2=\>a2[i]}] == nil  
res[{key1=\>a1[i], key2=\>a2[i]}] \<\< i  
end  
end  
return res  
end  
end

def testmy\_hash  
my\_hash={‘x’,[1,1,3,1,5,6],‘w’,[1,2,3,1,4,5],  
‘y’,[1,1,1,4,4,6],‘z’,[0,1,1,2,3,4]}  
my\_hash.extend MyHash  
res = my\_hash.fixed\_vars(‘x’,‘w’)  
puts res.inspect  
end

testmy\_hash

Best regards.
