Forum: Ruby In search of elegant indices searching

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.
67bb4df2775f6a6b603347dce7119571?d=identicon&s=25 unknown (Guest)
on 2006-05-16 11:23
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-05-16 17:17
(Received via mailing list)
2006/5/16, Nuralanur@aol.com <Nuralanur@aol.com>:
> 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
288733e11db1c6dbbf50871d2c6738c6?d=identicon&s=25 Uncutstone Wu (uncutstone)
on 2006-05-16 19:46
Nuralanur@aol.com å??é?ï¼?

> 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.[](key)
      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.
This topic is locked and can not be replied to.