RosettaCodeData/Task/Associative-array-Creation/Seed7/associative-array-creation....

46 lines
916 B
Plaintext

$ include "seed7_05.s7i";
# Define hash type
const type: myHashType is hash [string] integer;
# Define hash table
var myHashType: aHash is myHashType.value;
const proc: main is func
local
var string: stri is "";
var integer: number is 0;
begin
# Add elements
aHash @:= ["foo"] 42;
aHash @:= ["bar"] 100;
# Check presence of an element
if "foo" in aHash then
# Access an element
writeln(aHash["foo"]);
end if;
# Change an element
aHash @:= ["foo"] 7;
# Remove an element
excl(aHash, "foo");
# Loop over the hash values
for number range aHash do
writeln(number);
end for;
# Loop over the hash keys
for key stri range aHash do
writeln(stri);
end for;
# Loop over hash keys and values
for number key stri range aHash do
writeln("key: " <& stri <& ", value: " <& number);
end for;
end func;