Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,69 @@
with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;
procedure Merge_Maps is
use Ada.Text_Io;
type Key_Type is new String;
type Value_Type is new String;
package Maps is
new Ada.Containers.Indefinite_Ordered_Maps (Key_Type => Key_Type,
Element_Type => Value_Type);
use Maps;
function Merge (Original : Map; Update : Map) return Map is
Result : Map := Original;
Cur : Cursor := Update.First;
begin
while Has_Element (Cur) loop
if Original.Contains (Key (Cur)) then
Result.Replace_Element (Result.Find (Key (Cur)),
Element (Cur));
else
Result.Insert (Key (Cur), Element (Cur));
end if;
Next (Cur);
end loop;
return Result;
end Merge;
procedure Put_Map (M : Map) is
Cur : Cursor := M.First;
begin
while Has_Element (Cur) loop
Put (String (Key (Cur)));
Set_Col (12);
Put (String (Element (Cur)));
New_Line;
Next (Cur);
end loop;
end Put_Map;
Original : Map;
Update : Map;
Result : Map;
begin
Original.Insert ("name", "Rocket Skates");
Original.Insert ("price", "12.75");
Original.Insert ("color", "yellow");
Update.Insert ("price", "15.25");
Update.Insert ("color", "red");
Update.Insert ("year", "1974");
Result := Merge (Original, Update);
Put_Line ("Original:");
Put_Map (Original);
New_Line;
Put_Line ("Update:");
Put_Map (Update);
New_Line;
Put_Line ("Result of merge:");
Put_Map (Result);
New_Line;
end Merge_Maps;

View file

@ -0,0 +1,14 @@
(import std.Dict)
(let base (dict
"name" "Rocket Skates"
"price" "12.75"
"color" "yellow"))
(let update (dict
"price" "15.25"
"color" "red"
"year" "1974"))
(mut result (dict:copy base))
(dict:update! result update)
(print result)

View file

@ -1,4 +1,4 @@
-- 23 Aug 2025
-- 21 Feb 2026
include Setting
say 'ASSOCIATIVE ARRAY: MERGE'
@ -10,7 +10,7 @@ call CreateUpdate
call ShowUpdate
call MergeBaseUpdate
call ShowBase 'Merged'
call DumpVariables
call Showvars
exit
CreateBase:
@ -76,4 +76,5 @@ if WordPos(k,upda) = 0 then
upda.k=v
return
-- Showvars
include Math

View file

@ -0,0 +1,5 @@
set dict1 [dict create name "Rocket Skates" price 12.75 color yellow]
set dict2 [dict create price 15.25 color red year 1974]
dict for {key val} [dict merge $dict1 $dict2] {
puts "$key: $val"
}

View file

@ -0,0 +1,10 @@
set arrayname(item) $value
# or set a series of item value pairs at one time:
# create initial array
array set skates {
name {Rocket Skates}
price 12.75
color yellow
}

View file

@ -0,0 +1,39 @@
# print formatted output
proc print_array {fields arr} {
# list back to array
array set A $arr
set names [array names A]
foreach key $fields {
# format output
puts -nonewline stdout [format "%-10s" $key]
if {$key in $names} {
puts stdout [format "%-16s" $A($key)]
} else {
puts stdout ""
}
}
}
# set field order
set fields [list name price color year]
puts "skates"
print_array $fields [array get skates]
# list of updates {arrayname item value}
set updates {
{skates price 15.15}
{skates color red}
{skates year 1974}
}
# update array
foreach update $updates {
lassign $update item field value
array set $item [list $field $value]
}
puts [string repeat "-" 24]
puts "skates updated"
print_array $fields [array get skates]

View file

@ -0,0 +1,29 @@
set d1=createobject("Scripting.Dictionary")
d1.add "name", "Rocket Skates"
d1.add "price", 12.75
d1.add "color", "yellow"
set d2=createobject("Scripting.Dictionary")
d2.add "price", 15.25
d2.add "color", "red"
d2.add "year", 1974
set d3=createobject("Scripting.Dictionary")
for each k1 in d1.keys
if not d3.exists(k1) then
d3.add k1, d1(k1)
else
d3(k1)=d1(k1)
end if
next
for each k2 in d2.keys
if not d3.exists(k2) then
d3.add k2, d2(k2)
else
d3(k2)=d2(k2)
end if
next
for each k3 in d3.keys
wscript.echo k3 & vbtab & d3(k3)
next