LB Booster
« array operations »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 04:18am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: array operations  (Read 557 times)
Alincon
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 147
xx 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.
User IP Logged

tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx 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 » User IP Logged

Alincon
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 147
xx 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.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx 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.

User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx 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.
User IP Logged

Alincon
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 147
xx 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)
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx 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.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls