Re: [External] How to compare a hash key with a string value
"Tran, Minh" <[email protected]> Wed, 23 Oct 2019 18:33:17 +0000
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <SN6PR05MB53917E6EF772F71DAECC2498EE6B0@SN6PR05MB5391.namprd05.prod.outlook.com> |
Thank you so much Rob for all of the helps. Yes, it works and I learn a lot from you and other members more on Ruby syntax for the hash. Regards, Minh ________________________________ From: ruby-talk <[email protected]> on behalf of Rob Biedenharn <[email protected]> Sent: Wednesday, October 23, 2019 2:28 PM To: Ruby users <[email protected]> Subject: Re: [External] How to compare a hash key with a string value I think I see your problem, Minh. Your hashes (elements of your array of hashes, actually), seems to be in JSON format and doesn't produce the Ruby Hash with the keys that you expect. {"label":"Name"} is the same as { :label => "Name" } because the Ruby syntax for a Hash literal looks like {key: value} where the key is always a symbol. It is also true that :"key" is an alternate way of defining a Symbol literal. If you change your loop to be: hash.each.with_index(1) do |person,i| puts "Person is #{i}" if person.key? :width puts "The value of width is #{person[:width]}" end end I think that you'll get the result that you were expecting. Alternatively, you could replace the Hash literal syntax with: hash = [{"label" => "Name","value" => "Bob","identifier" => "field2","type" => "oneLineText","page" => 1,"page_name" => "Step 1","width" => "100%"}, {"label" => "Email","value" => "[email protected]<mailto:[email protected]>","identifier" => "field3","type" => "email","page" => 1,"page_name" => "Step 1","width" => "100%"}, {"label" => "Phone Number","value" => "","identifier" => "field7","type" => "oneLineText","page" => 1,"page_name" => "Step 1","width" => "100%"}, {"label" => "Comments","value" => "some information about the compagny","identifier" => "field5","type" => "textarea","page" => 1,"page_name" => "Step 1","width" => "100%"}, ] And your original code that expected keys that were Strings would work. -Rob On 2019-Oct-23, at 14:01 , Tran, Minh <[email protected]<mailto:[email protected]>> wrote: Thank you so much Jonathan for all of the helps. I tried your code and it gave me the person number such as person 1 , person 2, ete; but it does not give me the value of the person.has_key?('width') on each of the person. I still try to figure out why it did not execute this piece of code if person.has_key?('width') puts "The value of width is #{person['width']}" end My original program is as followed: hash = [{"label":"Name","value":"Bob","identifier":"field2","type":"oneLineText","page":1,"page_name":"Step 1","width":"100%"},{"label":"Email","value":"[email protected]<mailto:[email protected]>","identifier":"field3","type":"email","page":1,"page_name":"Step 1","width":"100%"},{"label":"Phone Number","value":"","identifier":"field7","type":"oneLineText","page":1,"page_name":"Step 1","width":"100%"},{"label":"Comments","value":"some information about the compagny","identifier":"field5","type":"textarea","page":1,"page_name":"Step 1","width":"100%"}] hash.each_with_index do |person, i| puts "Person is #{i}" if person.has_key?('width') puts "The value of width is #{person['width']}" end ________________________________ From: ruby-talk <[email protected]<mailto:[email protected]>> on behalf of Jonathan Fagan <[email protected]<mailto:[email protected]>> Sent: Wednesday, October 23, 2019 1:48 PM To: Ruby users <[email protected]<mailto:[email protected]>> Subject: [External] Re: How to compare a hash key with a string value I am not 100% clear on the question, but you could possibly just do something like this? hash.each_with_index do |person, i| puts "Person is #{i}" if person.has_key?('width') puts "The value of width is #{person['width']}" end end On Wed, Oct 23, 2019 at 10:36 AM Tran, Minh <[email protected]<mailto:[email protected]>> wrote: Hello, I am having a scenario which I want to print out the value of a hash element when its key equals to a string value such as "width". I try several alternatives but so far I have had a good result. So if you know on how to do , please share it with me. My hash table array looks like as the following: hash = [{"label":"Name","value":"Bob","identifier":"field2","type":"oneLineText","page":1,"page_name":"Step 1","width":"100%"},{"label":"Email","value":"[email protected]<mailto:[email protected]>","identifier":"field3","type":"email","page":1,"page_name":"Step 1","width":"100%"},{"label":"Phone Number","value":"","identifier":"field7","type":"oneLineText","page":1,"page_name":"Step 1","width":"100%"},{"label":"Comments","value":"some information about the compagny","identifier":"field5","type":"textarea","page":1,"page_name":"Step 1","width":"100%"}] My Ruby code is as the following where I need to print out the hash value when the hash key equals to "width" i = 0 hash.each do |person| i += 1 puts "Person is #{i}" person.each do |key, value| if key == "width" puts "The value of width is #{value}" end end end Thanks in advance for all of the helps. Dun ________________________________ This e-mail message (including any attachments) is for the sole use of the intended recipient(s) and may contain confidential and privileged information. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message (including any attachments) is strictly prohibited. If you have received this message in error, please contact the sender by reply e-mail message and destroy all copies of the original message (including attachments). Unsubscribe: <mailto:[email protected]<mailto:[email protected]>?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk> Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk> Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>