Author |
Topic: array operations (Read 557 times) |
|
Alincon
Full Member
member is offline
Posts: 147
|
|
array operations
« Thread started on: Jun 22nd, 2015, 3:47pm » |
|
I have a program that consists of several listboxes that, together, work like a spreadsheet. It does row and column totals and averages, etc. What I want to do is allow the user to enter something like A + B = C or C = A + B The program would then add the contents of each element in column A (listbox A) to the corresponding element in column B (listbox B) and place the results in the corresponding elements in column C (listbox C) (I'd like to do subtraction, multiplication and division, too) I was trying to use Eval statements in LB, but was unsuccessful. How can I use the array operation statements to do what I want in LBB?
r.m.
|
|
Logged
|
|
|
|
tsh73
Full Member
member is offline
Gender:
Posts: 210
|
|
Re: array operations
« Reply #1 on: Jun 22nd, 2015, 5:35pm » |
|
Code:dim a(5), b(5), c(5)
for i = 1 to 5
a(i)=i
b(i)=10*i
next
c()=a()+b()
gosub [printArr]
c()=b()-a()
gosub [printArr]
c()=a()*b()
gosub [printArr]
a(0)=1 'or we have division by zero - obviously array operations start with 0's item
c()=b()/a()
gosub [printArr]
end
[printArr]
for i = 1 to 5
print c(i);" ";
next
print
return
Als, for LBB specific EVAL things, see reply#5 of the The future of LBB - please read thread.
|
« Last Edit: Jun 22nd, 2015, 5:36pm by tsh73 » |
Logged
|
|
|
|
Alincon
Full Member
member is offline
Posts: 147
|
|
Re: array operations
« Reply #2 on: Jun 26th, 2015, 8:08pm » |
|
Okay, I see how to do math on arrays, but I guess I didn't make it clear that translating user input is a different problem. I don't want to do this: Code:
If userInput$ = "A+B=C" then C()=A()+B()
If userInput$ = "C=A+B" then C()=A()+B()
If userInput$ = "A*B=C" then C()=A()*B()
If userInput$ = "C=A*B" then C()=A()*B()
If userInput$ = "D+E=F" then D()=E()+F()
If userInput$ = "J=I/H" then J()=I()/H()
...
Can I use the EVAL statement to translate user input?
r.m.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: array operations
« Reply #3 on: Jun 27th, 2015, 06:37am » |
|
on Jun 26th, 2015, 8:08pm, Alincon wrote:Can I use the EVAL statement to translate user input? |
|
EVAL can only return a single numeric or string value, not an entire array, so you would have to evaluate each element individually in a loop. That would involve some fairly straightforward string manipulation to convert "A+B" into "A(i)+B(i)" for example.
Once you've evaluated each element you then have to assign those values to the output array (e.g. "C(i)"). You could use the FNassign kludge in the linked thread to do that.
So possible, but quite messy:
Code: dim A(10), B(10), C(10)
userInput$ = "C=A*B"
lhs$ = AddIndices$(word$(userInput$, 1, "="))
rhs$ = AddIndices$(word$(userInput$, 2, "="))
for i = 0 to 10
dummy = eval("FNassign("+lhs$+","+rhs$+")")
next
end
function AddIndices$(a$)
i = 1
while i <= len(a$)
if instr(" +-*/", mid$(a$,i,1)) then
i += 1
else
a$ = left$(a$,i) + "(i)" + mid$(a$,i+1)
i += 4
end if
wend
AddIndices$ = a$
end function
function assign(byref a, b)
a = b
end function Richard.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: array operations
« Reply #4 on: Jun 28th, 2015, 09:51am » |
|
Here's a more functional example:
Code: WindowWidth = 640
WindowHeight = 300
dim A(9),B(9),C(9),D(9),E(9),F(9),G(9),H(9),I(9)
nomainwin
for row = 0 to 5
x = 10
for col = 0 to 9
if col = 0 then
stylebits #w.tb _ES_CENTER, 0, 0, 0
w = 120
else
stylebits #w.tb _ES_READONLY, 0, 0, 0
w = 47
end if
textbox #w.tb, x, row*30+20, w, 20
x += w + 7
maphandle #w.tb, "#w.tb";row;col
next col
next row
stylebits #w.tb00 _ES_READONLY or _ES_CENTER, 0, 0, 0
button #w, "RECALCULATE", [recalc], UL, 260, 210
open "Array calculations" for window as #w
#w "trapclose [quit]"
#w.tb00 "index (i)"
#w.tb10 "A = i + 2"
#w.tb20 "B = SQR(A)"
#w.tb30 "C = A - B"
#w.tb40 "D = B * C"
#w.tb50 "E = D / A"
for col = 1 to 9
handle$ = "#w.tb0";col
#handle$ "!disable"
#handle$ col
next col
#w.tb10 "!setfocus"
[recalc]
for row = 1 to 5
handle$ = "#w.tb";row;0
#handle$ "!contents? userInput$"
if userInput$ <> "" then
lhs$ = AddIndices$(word$(userInput$, 1, "="))
rhs$ = AddIndices$(word$(userInput$, 2, "="))
for i = 1 to 9
handle$ = "#w.tb";row;i
try
dummy = eval("FNassign("+lhs$+","+rhs$+")")
#handle$ eval(lhs$)
catch
#handle$ Err$
end try
next i
end if
next row
wait
[quit]
close #w
end
function AddIndices$(a$)
i = 1
while i <= len(a$)
if (i = 1 or not(IsCap(mid$(a$, i-1, 1)))) and _
IsCap(mid$(a$, i, 1)) and _
not(IsCap(mid$(a$, i+1, 1))) then
a$ = left$(a$,i) + "(i)" + mid$(a$,i+1)
i += 3
end if
i += 1
wend
AddIndices$ = a$
end function
function IsCap(a$)
IsCap = (a$ >= "A" and a$ <= "Z")
end function
function assign(byref a, b)
a = b
end function Richard.
|
|
Logged
|
|
|
|
Alincon
Full Member
member is offline
Posts: 147
|
|
Re: array operations
« Reply #5 on: Jun 30th, 2015, 2:39pm » |
|
Thank you for taking the time to code this example. I will need some time to understand it, and try to apply it to my program.
r.m.
(in LB, the program aborts on the stylebits statements)
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: array operations
« Reply #6 on: Jun 30th, 2015, 3:05pm » |
|
on Jun 30th, 2015, 2:39pm, Alincon wrote:in LB, the program aborts on the stylebits statements |
|
Yes I know: I put a space where there should be a comma. But since the program cannot run in LB anyway, it doesn't really matter.
Richard.
|
|
Logged
|
|
|
|
|