From: Chad Perrin Date: 2011-08-25T02:58:23+09:00 Subject: Re: eliminate duplicate key based on value --gKMricLos+KVdGMg Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Aug 25, 2011 at 02:12:34AM +0900, Lucky Nl wrote: >=20 > i have hash as below >=20 > list =3D {"18" =3D> 1,"15" =3D> 2,"15" =3D> 3,"17" =3D> 4,"18" =3D> 5} Let's see what irb does with that: irb >> list =3D {"18" =3D> 1,"15" =3D> 2,"15" =3D> 3,"17" =3D> 4,"18" =3D> = 5} =3D> {"17"=3D>4, "15"=3D>3, "18"=3D>5} As you can see, subsequent assignments of a value to a particular key when that key has already been used simply overwrite the value already established for that key. Thus, if you want only the 2 value for the "15" key to exist, you need to set the 2 value last. If you set the 3 value last, it overwrites the 2 value. This is because there is only one of each key in a hash. How would you be able to access a particular value, otherwise? I hope that helps. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --gKMricLos+KVdGMg Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk5VN3gACgkQ9mn/Pj01uKWG2gCghUwZ2awoY3LZRPIuc94ahaju Bq0AoNc++2hO0ueV3Rxgk0fGaOKHnACW =mB0M -----END PGP SIGNATURE----- --gKMricLos+KVdGMg--