Anonymous user
Associative array/Creation: Difference between revisions
Languages sorted
(added C++) |
(Languages sorted) |
||
Line 92:
@hash->{'key1', 'three'} = ('val1', -238.83);
==[[Ruby]]==▼
#a hash object that returns nil for unknown keys▼
hash={}▼
hash[666]='devil'▼
hash[777] # => nil▼
hash[666] # => 'devil'▼
#a hash object that returns 'unknown key' for unknown keys▼
hash=Hash.new('unknown key')▼
hash[666]='devil'▼
hash[777] # => 'unknown key'▼
hash[666] # => 'devil'▼
#a hash object that returns "unknown key #{key}" for unknown keys▼
hash=Hash.new{|h,k|h[k]="unknown key #{k}"}▼
hash[666]='devil'▼
hash[777] # => 'unknown key 777'▼
hash[666] # => 'devil'▼
==[[Python]]==
Line 138 ⟶ 119:
Create a generic mapping function that applys a callback to elements in a list:
▲==[[Ruby]]==
▲ #a hash object that returns nil for unknown keys
▲ hash={}
▲ hash[666]='devil'
▲ hash[777] # => nil
▲ hash[666] # => 'devil'
▲ #a hash object that returns 'unknown key' for unknown keys
▲ hash=Hash.new('unknown key')
▲ hash[666]='devil'
▲ hash[777] # => 'unknown key'
▲ hash[666] # => 'devil'
▲ #a hash object that returns "unknown key #{key}" for unknown keys
▲ hash=Hash.new{|h,k|h[k]="unknown key #{k}"}
▲ hash[666]='devil'
▲ hash[777] # => 'unknown key 777'
▲ hash[666] # => 'devil'
|