Demonstrating the principle though not optimized.
'================
'AssociativeArray
'================
indexbase 1 'default
dim string n[1000]
dim string d[1000]
int i,e
'
function dat(string s1,s2)
for i=1 to e
if n[i]=s1
d[i]=s2
return
endif
next
'new element
e++
n[e]=s1
d[e]=s2
end function
'
function dat(string s1) as string
for i=1 to e
if n[i]=s1
return d[i]
endif
next
end function
'
'TEST
'====
'Using Dat(n) as a pseudo variable
dat("A")="1"
dat("B")="2"
print dat("A")
dat("A")=10
print dat("A")
print e
This could be useful...
but Charles you made two function with same name dat()
one have 2 arguments and other one ..so what is a purpose?
ps..
...and results are
1 , 10 , 2
...is that correct?
if i understand it e is 2
I think it is known in the software business as 'function overloading'. The functions have the same name but their parameters are different. When you call a function the compiler picks the function that best matches the parameters. In this case the first function with 2 params is for writing, and the second function with 1 param is for reading.
To make the pseudo variable work, there is another trick:
dat(s1)=s2
is equivalent to:
dat(s1,s2)
You can also do this with the mid function
mid(s1,i)=s2
is the same as:
mid(s1,i,s2)
(yes e should be 2 indicating correct storage of data)
QuoteI think it is known in the software business as 'function overloading
Aha ..i see ..thanks on explanation Charles.. ;)
well i really never do this thing
My example for o2 and associative array
' only the telephone number of alice is not found :-)
uses console
string cr = chr(13,10)
' Creating arrays to store names and telephone numbers
Dim names(4) As String
Dim telephone_numbers(4) As String
' Initializing the arrays with names and telephone numbers
names(1) = "Alice"
telephone_numbers(1) = "123-456-7890"
names(2) = "Bob"
telephone_numbers(2) = "987-654-3210"
names(3) = "Charlie"
telephone_numbers(3) = "555-123-4567"
names(4) = "David"
telephone_numbers(4) = "111-222-3333"
' Function to find the telephone number by name
Function FindTelephoneNumber(namey As String) As String
Dim names(4) As String
Dim telephone_numbers(4) As String
int i
For i = 1 To 4
If names(i) = namey Then
Return telephone_numbers(i)
End If
Next
Return "Not found"
End Function
' Accessing a telephone number by name
Dim namey As String = "Alice"
Dim telephone_number As String
telephone_number = FindTelephoneNumber(namey)
Print "The telephone number of " + namey + " is " + telephone_number + "." cr
' Adding a new entry to the phone book
names(5) = "Eve"
telephone_numbers(5) = "444-333-2222"
' Updating an existing entry
telephone_numbers(2) = "999-888-7777"
' Removing an entry (by setting it to empty)
names(3) = ""
telephone_numbers(3) = ""
' Printing the entire phone book
Print "Phone Book:"
int i
For i = 1 To 5
If names(i) <> "" Then
Print names(i) + ": " + telephone_numbers(i) cr
End If
Next
' Waiting for the user to press a key before closing
wait
I have made an o2 -> modified example for better understanding with Telephon Numbers and names..
in general would be great to make some more example with oxygen with explanations and comments, thx, frank
'
'================
'AssociativeArray
'================
indexbase 1 'default
dim string n[1000] ' Array to store names
dim string d[1000] ' Array to store telephone numbers
int i, e
' Function to add or update a telephone number for a given name
function dat(string name, phone)
for i = 1 to e
if n[i] = name then
d[i] = phone
return
endif
next
' If the name is not found, add here a new entry
e = e + 1
n[e] = name
d[e] = phone
end function
' Function to retrieve the telephone number for a given name
function dat(string name) as string
for i = 1 to e
if n[i] = name then
return d[i]
endif
next
return "Number not found" ' Return here a message if the name is not found
end function
'
' TEST
'====
' Using Dat(n) as a pseudo variable to add and retrieve telephone numbers
' Adding telephone numbers
dat("Alice", "123-456-7890")
dat("Bob", "987-654-3210")
dat("Charles", "555-123-4567")
' Retrieving and printing telephone numbers
print dat("Alice") ' Output: 123-456-7890
print dat("Bob") ' Output: 987-654-3210
' Updating a telephone number
dat("Alice", "111-222-3333")
print dat("Alice") ' Output: 111-222-3333
' Trying to retrieve a number for a name that doesn't exist
print dat("Eve") ' Output: Number not found
' Print the total number of entries
print e ' Output: 3