module "/pliant/language/unsafe.pli" var (Dictionary Str Int) age # insertion implicit age insert "Nat" 24 insert "Jules" 25 insert "Josh" 17 # when you know keys already exist: age "Nat" := 24 age "Jules" := 25 age "Josh" := 17 #----------------------------- var (Dictionary Str Str) food_color implicit food_color insert "Apple" "red" insert "Banana" "yellow" insert "Lemon" "yellow" insert "Carrot" "orange" #----------------------------- |
#----------------------------- dict insert key value #----------------------------- # food_color defined per the introduction food_color insert "Raspberry" "pink" console "Known foods:" eol each x food_color console (food_color key x) eol Known foods: Banana Apple Raspberry Carrot #----------------------------- |
#----------------------------- # does dict have a value for key ? if (dict exists key) # it exists else # it doesn't #----------------------------- # food_color per the introduction var List:Str fruits; fruits += "Banana"; fruits += "Martini" each name fruits if (food_color exists name) console name " is a food." eol else console name " is a drink." eol Banana is a food. Martini is a drink. #----------------------------- age := new (Dictionary Str Int) implicit age insert "Toddler" 3 insert "Unborn" 0 insert "Phantasm" undefined var List:Str things things+="Toddler", things+="Phantasm"; things+="Relic" each thing things console thing ": " if (age exists thing) console "Exists " console (shunt age:thing<>undefined "Defined " "") console (shunt age:thing>0 "True " "") console eol # Toddler: Exists Defined True # Unborn: Exists Defined # Phantasm: Exists # Relic: #----------------------------- |
#----------------------------- # remove the first element in dict with key value dict -= dict value #----------------------------- # food_color as per Introduction function print_foods each food food_color console "key : " (food_color key food) ", value: " (shunt food="" "(undef)" food) eol console eol console "Initially:" eol print_foods console "With Banana undef" eol food_color "Banana" := "" # no true way to undef a Str... print_foods console "With Banana deleted" eol food_color -= food_color "Banana" print_foods # Initially: # key : Lemon, value: yellow # key : Banana, value: yellow # key : Apple, value: red # key : Carrot, value: orange # # With Banana undef # key : Lemon, value: yellow # key : Banana, value: (undef) # key : Apple, value: red # key : Carrot, value: orange # # With Banana deleted # key : Lemon, value: yellow # key : Apple, value: red # key : Carrot, value: orange #--------------------------- #----------------------------- each value dict # the key is (dict key value) #----------------------------- # food_color per the introduction each color food_color console (food_color key color) " is " color eol # Banana is yellow. # Apple is red. # Carrot is orange. # Lemon is yellow. #----------------------------- var Pointer:Str color :> food_color first while exists:color console (food_color key color) " is " color color :> food_color next color #--------------------------- |