Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8738451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:44:02+00:00 2026-06-13T10:44:02+00:00

Given a hash – hash = { 1 => {ID => [NUMBER, 11] },

  • 0

Given a hash –

hash = { 
         1 => {"ID"         => ["NUMBER", 11]   }, 
         2 => {"TITLE"      => ["VARCHAR2", 5]  },
         3 => {"FIRST_NAME" => ["VARCHAR2", 50] }, 
         4 => {"LAST_NAME"  => ["VARCHAR2", 50] },         
         5 => {"BIRTH_DATE" => ["DATE", -2]     }
       }  

and 2 input parameters – "FIRST_NAME" and ["VARCHAR2",50].

What is the most elegant way to do –

  1. check whether "FIRST_NAME" exists as key of any nested hash.
  2. And if it exists whether value of hash[3]["FIRST_NAME"] is equal to second parameter i.e. ["VARCHAR2",50].
  3. And if these 2 parameters match then return the key whose value is this nested hash i.e. 3 in this case

Currently I do the following –

array = hash.values.map {|h| h.to_a}.flatten(2)
puts hash.key(Hash["FIRST_NAME",["VARCHAR2",50]]) if !(index = array.index("FIRST_NAME")).nil? ? array[index+1] == ["VARCHAR2",50] : false # 3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:44:03+00:00Added an answer on June 13, 2026 at 10:44 am

    You will find a lot of information in you check the hash api.

    a = 'FIRST_NAME'
    b = ['VARCHAR2', 50]
    
    hash.each do |k, v|
      if v.key?(a)            # ①
        return k if v[a] == b # ② and ③
      end
    end
    

    You could also write it as below actually because if it does not contain the key, it will never match and just go to the next occurence.

    hash.each do |k, v|
      return k if v[a] == b
    end
    

    Your example only show hash with one pair, if there is more your need to do a new each on v.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the Ruby code line = first_name=mickey;last_name=mouse;country=usa record = Hash[*line.split(/=|;/)] I understand everything in
I've been looking for a way to hash a given string in C# that
Given a custom, new-style python class instance, what is a good way to hash
Given the hash of a blob, is there a way to get a list
Is there any quick way to get a (random) permutation of a given hash?
Given I have the below clients hash, is there a quick ruby way (without
Given a data structure (e.g. a hash of hashes), what's the clean/recommended way to
Given this hash that comes though :params positions=>{ 1=>{x=>50, y=>30}, 2=>{x=>22, y=>53}, 3=>{x=>68, y=>35}
Given this hash in ruby: h={ 2010-03-01=>2 2010-03-02=>4 2010=03-03=>1 .. .. n days with
Given the following code #!/usr/bin/perl use Data::Dumper; my %hash; my @colos = qw(ac4 ch1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.