代码之家  ›  专栏  ›  技术社区  ›  Rajagopalan

合并三个散列并得到结果散列

  •  -1
  • Rajagopalan  · 技术社区  · 6 年前

    我已经阅读了xls并形成了这三个散列

    hash1=[{'name'=>'Firstname',
            'Locator'=>'id=xxx',
            'Action'=>'TypeAndWait'},
           {'name'=>'Password',
            'Locator'=>'id=yyy',
            'Action'=>'TypeAndTab'}]
    

    hash2=[{'Test Name'=>'Example',
            'TestNumber'=>'Test1'},
           {'Test Name'=>'Example',
            'TestNumber'=>'Test2'}]
    

    我的第三个杂烩

    hash3=[{'name'=>'Firstname',
            'Test1'=>'four',
            'Test2'=>'Five',
            'Test3'=>'Six'},
           {'name'=>'Password',
            'Test1'=>'Vicky',
            'Test2'=>'Sujin',
            'Test3'=>'Sivaram'}]
    

    result={"Example"=>
             {"Test1"=>
               {'Firstname'=>
                 ["id=xxx","four", "TypeAndWait"],
                'Password'=>
                 ["id=yyy","Vicky", "TypeAndTab"]},
              "Test2"=>
               {'Firstname'=>
                 ["id=xxx","Five", "TypeAndWait"],
                'Password'=>
                 ["id=yyy","Sujin", "TypeAndTab"]}}}
    

    我得到了这个结果,但我不得不在我的程序中写60行代码,但我不认为我必须写这么长的程序当我使用Ruby,我坚信一些简单的方法来实现这一点。有人能帮我吗?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Cary Swoveland    6 年前

    我们有三个数组,我已经重新命名了 arr1 arr2 arr3 hash1 , hash2 hash3 对于数组来说不是特别好的名字。:-))

    arr1 = [{'name'=>'Firstname', 'Locator'=>'id=xxx', 'Action'=>'TypeAndWait'},
            {'name'=>'Password', 'Locator'=>'id=yyy', 'Action'=>'TypeAndTab'}]
    
    arr2 = [{'Test Name'=>'Example', 'TestNumber'=>'Test1'},
            {'Test Name'=>'Example', 'TestNumber'=>'Test2'}]
    
    arr3=[{'name'=>'Firstname', 'Test1'=>'four', 'Test2'=>'Five', 'Test3'=>'Six'},
          {'name'=>'Password', 'Test1'=>'Vicky', 'Test2'=>'Sujin', 'Test3'=>'Sivaram'}]
    

    "Test1" "Test2" 在作为 . 数组中不需要其他任何东西,所以让我们提取这些值(其中可以有任意数,但这里只有两个)。

    a2 = arr2.map { |h| h['TestNumber'] }
      #=> ["Test1", "Test2"]
    

    接下来我们需要重新排列 阿里尔3 通过创建一个散列,其键是 a2 .

    h3 = a2.each_with_object({}) { |test,h|
      h[test] = arr3.each_with_object({}) { |f,g| g[f['name']] = f[test] } }
      #=> {"Test1"=>{"Firstname"=>"four", "Password"=>"Vicky"},
      #    "Test2"=>{"Firstname"=>"Five", "Password"=>"Sujin"}}
    

    接下来我们需要重新安排 阿里尔1 通过创建其键与 h3 .

    h1 = arr1.each_with_object({}) { |g,h| h[g['name']] = g.reject { |k,_| k == 'name' } }
      #=> {"Firstname"=>{"Locator"=>"id=xxx", "Action"=>"TypeAndWait"}, 
      #    "Password"=>{"Locator"=>"id=yyy", "Action"=>"TypeAndTab"}}
    

    { 'Example'=>
      a2.each_with_object({}) do |test,h|
        h[test] = h3[test].each_with_object({}) do |(k,v),g|
          f = h1[k]
          g[k] = [f['Locator'], v, f['Action']]
        end
      end
    }
      #=> {"Example"=>
      #     {"Test1"=>{"Firstname"=>["id=xxx", "four", "TypeAndWait"],
      #                "Password"=>["id=yyy", "Vicky", "TypeAndTab"]},
      #      "Test2"=>{"Firstname"=>["id=xxx", "Five", "TypeAndWait"],
      #                "Password"=>["id=yyy", "Sujin", "TypeAndTab"]}}}
    
        2
  •  1
  •   Aleksei Matiushkin    6 年前

    你叫什么 hash{1-2-3} 首先是数组。另外,我很肯定你打错了 hash1#Locator 和/或 hash3#name . 下面的代码适用于这个精确的数据,但是要更新它以反映任何更改应该不难。

    hash2.
      map(&:values).
      group_by(&:shift).
      map do |k, v|
        [k, v.flatten.map do |k, v|
              [k, hash3.map do |h3|
                    # lookup a hash from hash1
                    h1 = hash1.find do |h1|
                       h3['name'].start_with?(h1['Locator'])
                    end
                    # can it be nil btw?
                    [
                      h1['name'],
                      [
                        h3['name'][/.*(?=-id)/],
                        h3[k],
                        h1['Action']
                      ]
                    ]
              end.to_h]
        end.to_h]
      end.to_h
    
    推荐文章