Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -22,31 +22,33 @@ BEGIN
a
END # UPDATE # ;
# construct the associative arrays for the task #
REF AARRAY a := INIT LOC AARRAY;
REF AARRAY b := INIT LOC AARRAY;
a // "name" := "Rocket Skates";
a // "price" := 12.75;
a // "color" := "yellow";
b // "price" := 15.25;
b // "color" := "red";
b // "year" := 1974;
# merge the arrays #
REF AARRAY c := INIT LOC AARRAY;
c UPDATE a UPDATE b;
# show the merged array #
REF AAELEMENT e := FIRST c;
WHILE e ISNT nil element DO
print( ( key OF e
, ": "
, CASE value OF e
IN (STRING s): s
, (INT i): whole( i, 0 )
, (REAL r): fixed( r, -12, 2 )
OUT "????"
ESAC
, newline
)
);
e := NEXT c
OD
BEGIN # tests #
# construct the associative arrays for the task #
REF AARRAY a := INIT LOC AARRAY;
REF AARRAY b := INIT LOC AARRAY;
a // "name" := "Rocket Skates";
a // "price" := 12.75;
a // "color" := "yellow";
b // "price" := 15.25;
b // "color" := "red";
b // "year" := 1974;
# merge the arrays #
REF AARRAY c := INIT LOC AARRAY;
c UPDATE a UPDATE b;
# show the merged array #
REF AAELEMENT e := FIRST c;
WHILE e ISNT nil element DO
print( ( key OF e
, ": "
, CASE value OF e
IN (STRING s): s
, (INT i): whole( i, 0 )
, (REAL r): fixed( r, -12, 2 )
OUT "????"
ESAC
, newline
)
);
e := NEXT c
OD
END

View file

@ -0,0 +1,36 @@
dim original(3, 2)
original[0, 0] = "name": original[0, 1] = "Rocket Skates"
original[1, 0] = "price": original[1, 1] = "12.75"
original[2, 0] = "color": original[2, 1] = "yellow"
dim update(3, 2)
update[0, 0] = "price": update[0, 1] = "15.25"
update[1, 0] = "color": update[1, 1] = "red"
update[2, 0] = "year": update[2, 1] = "1974"
dim merged(6, 2)
for i = 0 to 2
merged[i, 0] = update[i, 0]
merged[i, 1] = update[i, 1]
next i
index = 3
for i = 0 to 2
found = False
for j = 0 to 2
if original[i, 0] = update[j, 0] then
found = True
exit for
end if
next j
if not found then
merged[index, 0] = original[i, 0]
merged[index, 1] = original[i, 1]
index += 1
end if
next i
for i = 0 to index - 1
print "key: "; merged[i, 0]; ", value: "; merged[i, 1]
next i

View file

@ -0,0 +1,39 @@
100 cls
110 dim original$(3,2)
120 original$(0,0) = "name"
130 original$(0,1) = "Rocket Skates"
140 original$(1,0) = "price"
150 original$(1,1) = "12.75"
160 original$(2,0) = "color"
170 original$(2,1) = "yellow"
180 dim update$(3,2)
190 update$(0,0) = "price"
200 update$(0,1) = "15.25"
210 update$(1,0) = "color"
220 update$(1,1) = "red"
230 update$(2,0) = "year"
240 update$(2,1) = "1974"
250 dim merged$(6,2)
270 for i = 0 to 2
280 merged$(i,0) = update$(i,0)
290 merged$(i,1) = update$(i,1)
300 next i
320 index = 3
330 for i = 0 to 2
340 found = 0
350 for j = 0 to 2
360 if original$(i,0) = update$(j,0) then
370 found = 1
380 goto 400
390 endif
400 next j
410 if found = 0 then
420 merged$(index,0) = original$(i,0)
430 merged$(index,1) = original$(i,1)
440 index = index+1
450 endif
460 next i
480 for i = 0 to index-1
490 print "key: ";merged$(i,0);", value: ";merged$(i,1)
500 next i
510 end

View file

@ -0,0 +1,17 @@
base$[][] = [ [ "name" "Rocket Skates" ] [ "price" 12.75 ] [ "color" "yellow" ] ]
update$[][] = [ [ "price" 15.25 ] [ "color" "red" ] [ "year" 1974 ] ]
proc update . a$[][] b$[][] .
for b to len b$[][]
for a to len a$[][]
if a$[a][1] = b$[b][1]
a$[a][2] = b$[b][2]
break 1
.
.
if a > len a$[][]
a$[][] &= b$[b][]
.
.
.
update base$[][] update$[][]
print base$[][]

View file

@ -0,0 +1,47 @@
Type Dictionary
As String key
As String value
End Type
Sub merge(original() As Dictionary, update() As Dictionary, result() As Dictionary)
Dim As Integer i, j, index
Dim As Boolean found
For i = 0 To Ubound(update)
result(i) = update(i)
Next i
index = i
For i = 0 To Ubound(original)
found = False
For j = 0 To Ubound(update)
If original(i).key = update(j).key Then
found = True
Exit For
End If
Next j
If Not found Then
result(index) = original(i)
index += 1
End If
Next i
End Sub
Dim As Dictionary original(2)
original(0).key = "name": original(0).value = "Rocket Skates"
original(1).key = "price": original(1).value = "12.75"
original(2).key = "color": original(2).value = "yellow"
Dim As Dictionary update(2)
update(0).key = "price": update(0).value = "15.25"
update(1).key = "color": update(1).value = "red"
update(2).key = "year": update(2).value = "1974"
Dim As Dictionary merged(Ubound(update) + Ubound(original) - 1)
merge(original(), update(), merged())
For i As Integer = 0 To Ubound(merged)
Print "key: "; merged(i).key; ", value: "; merged(i).value
Next i
Sleep

View file

@ -0,0 +1,10 @@
void local fn Doit
CFDictionaryRef base = @{ @"name":@"Rocket Skates", @"price":@12.75 }
CFDictionaryRef update = @{ @"price":@15.25, @"color":@"red", @"year":@1974 }
CFDictionaryRef result = concat( base, update )
print result
end fn
fn DoIt
HandleEvents

View file

@ -0,0 +1,32 @@
100 CLS
110 DIM O$(3,2)
120 O$(0,0) = "name"
130 O$(0,1) = "Rocket Skates"
140 O$(1,0) = "price"
150 O$(1,1) = "12.75"
160 O$(2,0) = "color"
170 O$(2,1) = "yellow"
180 DIM U$(3,2)
190 U$(0,0) = "price"
200 U$(0,1) = "15.25"
210 U$(1,0) = "color"
220 U$(1,1) = "red"
230 U$(2,0) = "year"
240 U$(2,1) = "1974"
250 DIM M$(6,2)
270 FOR I = 0 TO 2
280 M$(I,0) = U$(I,0)
290 M$(I,1) = U$(I,1)
300 NEXT I
320 INDEX = 3
330 FOR I = 0 TO 2
340 FOUND = 0
350 FOR J = 0 TO 2
360 IF O$(I,0) = U$(J,0) THEN FOUND = 1 : GOTO 370
370 NEXT J
380 IF FOUND = 0 THEN M$(INDEX,0) = O$(I,0) : M$(INDEX,1) = O$(I,1) : INDEX = INDEX+1
390 NEXT I
410 FOR I = 0 TO INDEX-1
420 PRINT "key: ";M$(I,0);", value: ";M$(I,1)
430 NEXT I
440 END

View file

@ -0,0 +1,27 @@
(setq data1 '("name" "Rocket Skates"
"price" 12.75
"color" "yellow"))
(setq data2 '("price" 15.25
"color" "red"
"year" 1974))
(define (list->alist lst) (explode lst 2))
(macro (aset! Alist Key Val)
(local (E-Message)
(unless
(catch
(setf (assoc Key Alist) (list ($it 0) Val))
'E-Message)
(setf Alist (cons (list Key Val) Alist)))))
(define (foo list1 list2)
(let (out (list->alist list1))
(dolist (a (list->alist list2))
(aset! out (a 0) (string (a 1))))
(println out "\n")
(dolist (a out)
(println (format "%-5s %s" a)))))
(foo data1 data2)

View file

@ -0,0 +1,42 @@
(setq data1 '("name" "Rocket Skates"
"price" 12.75
"color" "yellow"))
(setq data2 '("price" 3.20
"color" "red"
"year" 1974))
(macro (ainc! Alist Key Val Func)
(local (E-Message)
(unless Func (set 'Func +))
(unless
(catch
(setf (assoc Key Alist)
(list ($it 0) (Func (or Val 1) ($it 1))))
'E-Message)
(setf Alist (cons (list Key (or Val 1)) Alist)))))
(define (list->alist xs)
(collect
(and (true? xs)
(if (= "color" (xs 0))
(list (pop xs) (list (pop xs)))
(list (pop xs) (pop xs))))))
(define (bu) (bind (apply unify $args)))
(define (foo list1 list2 , K V)
(let (out (list->alist list1))
(dolist (a (list->alist list2))
(bu '(K V) a)
(case K
("price" (ainc! out K V add))
("color" (ainc! out K V append))
;; ainc! can even be used to make a new entry or replace old value.
(true (ainc! out K V or))))
(println out "\n")
(dolist (a out)
(bu '(K V) a)
(println (format "%-5s " K) V))))
(foo data1 data2)

View file

@ -0,0 +1,4 @@
const base = {name: 'Rocket Skates' price: 12.75 color: 'yellow'}
const update = {price: 15.25 color: 'red' year: 1974}
$base | merge $update

View file

@ -0,0 +1,14 @@
begin
var base := Dict(('name','Rocket Skates'),
('price','12.75'),
('color','yellow'));
var update := Dict(('price','15.25'),
('color','red'),
('year','1974'));
var merged := new Dictionary<string,string>;
foreach var kv in base.Concat(update) do
merged[kv.Key] := kv.Value;
merged.PrintLines
end.

View file

@ -0,0 +1,47 @@
DECLARE SUB merge (original() AS ANY, update() AS ANY, result() AS ANY)
TYPE Dictionary
keyy AS STRING * 5
value AS STRING * 13
END TYPE
DIM original(2) AS Dictionary
original(0).keyy = "name": original(0).value = "Rocket Skates"
original(1).keyy = "price": original(1).value = "12.75"
original(2).keyy = "color": original(2).value = "yellow"
DIM update(2) AS Dictionary
update(0).keyy = "price": update(0).value = "15.25"
update(1).keyy = "color": update(1).value = "red"
update(2).keyy = "year": update(2).value = "1974"
DIM merged(UBOUND(update) + UBOUND(original) - 1) AS Dictionary
CALL merge(original(), update(), merged())
FOR i = 0 TO UBOUND(merged)
PRINT "keyy: "; merged(i).keyy; ", value: "; merged(i).value
NEXT i
END
SUB merge (original() AS Dictionary, update() AS Dictionary, result() AS Dictionary)
DIM i AS INTEGER, j AS INTEGER, index AS INTEGER, found AS INTEGER
FOR i = 0 TO UBOUND(update)
result(i) = update(i)
NEXT i
index = i
FOR i = 0 TO UBOUND(original)
found = 0
FOR j = 0 TO UBOUND(update)
IF original(i).keyy = update(j).keyy THEN
found = 1
EXIT FOR
END IF
NEXT j
IF found = 0 THEN
result(index) = original(i)
index = index + 1
END IF
NEXT i
END SUB

View file

@ -0,0 +1,44 @@
PROGRAM "Associative array/Merging"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
DIM original$[3, 2]
original$[0, 0] = "name": original$[0, 1] = "Rocket Skates"
original$[1, 0] = "price": original$[1, 1] = "12.75"
original$[2, 0] = "color": original$[2, 1] = "yellow"
DIM update$[3, 2]
update$[0, 0] = "price": update$[0, 1] = "15.25"
update$[1, 0] = "color": update$[1, 1] = "red"
update$[2, 0] = "year": update$[2, 1] = "1974"
DIM merged$[6, 2]
FOR i = 0 TO 2
merged$[i, 0] = update$[i, 0]
merged$[i, 1] = update$[i, 1]
NEXT i
index = 3
FOR i = 0 TO 2
found = 0
FOR j = 0 TO 2
IF original$[i, 0] = update$[j, 0] THEN
found = 1
EXIT FOR
END IF
NEXT j
IF found = 0 THEN
merged$[index, 0] = original$[i, 0]
merged$[index, 1] = original$[i, 1]
INC index
END IF
NEXT i
FOR i = 0 TO index - 1
PRINT "key: "; merged$[i, 0]; ", value: "; merged$[i, 1]
NEXT i
END FUNCTION
END PROGRAM

View file

@ -0,0 +1,42 @@
dim original$(3, 2)
original$(0, 0) = "name"
original$(0, 1) = "Rocket Skates"
original$(1, 0) = "price"
original$(1, 1) = "12.75"
original$(2, 0) = "color"
original$(2, 1) = "yellow"
dim update$(3, 2)
update$(0, 0) = "price"
update$(0, 1) = "15.25"
update$(1, 0) = "color"
update$(1, 1) = "red"
update$(2, 0) = "year"
update$(2, 1) = "1974"
dim merged$(6, 2)
for i = 0 to 2
merged$(i, 0) = update$(i, 0)
merged$(i, 1) = update$(i, 1)
next i
index = 3
for i = 0 to 2
found = 0
for j = 0 to 2
if original$(i, 0) = update$(j, 0) then
found = 1
break
end if
next j
if found = 0 then
merged$(index, 0) = original$(i, 0)
merged$(index, 1) = original$(i, 1)
index = index + 1
end if
next i
for i = 0 to index - 1
print "key: ", merged$(i, 0), ", value: ", merged$(i, 1)
next i