Login
|
Signup
langref.org
-
ruby
and
clojure
add..
all
cpp
csharp
erlang
fantom
fsharp
go
groovy
haskell
java
ocaml
perl
php
python
scala
Home
All
Solved
Unsolved
Strings
Numbers
Regex
Lists
Maps
Structure
Files
Dates
OOP
Networking
XML
Algorithms
Misc
Parallel
View Problem
Maps
Access
Check if a key exists in a map
Given a map pets
{joe:cat,mary:turtle,bill:canary}
print
"ok"
if an pet exists for
"mary"
ruby
puts "ok" if map.has_key?('mary')
map = {'joe'=>'cat', 'mary'=>'turtle', 'bill'=>'canary'}
puts "ok" if map.has_key?('mary')
ruby
puts "ok" if map['mary'] # Only works if map entry can't be nil or false
map = {'a'=>0, 'b'=>nil, 'c'=>42}
puts "ok" if map['mary'] # Only works if map entry can't be nil or false
clojure
(if (contains? '{joe cat mary turtle bill canary} 'mary)
(println "ok"))
(if (contains? '{joe cat mary turtle bill canary} 'mary)
(println "ok"))
Submit a new solution for
ruby
or
clojure
There are 25 other solutions in
additional
languages (
cpp
,
erlang
,
fantom
,
fsharp
...)