• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

2D String array -- did not work

Started by Chris Chancellor, June 03, 2018, 07:07:05 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello Charles,

my program which attempts to construct a 2D String array using Class method but did not work.

what's wrong with it ?  could you help me please ?

the program below can be compiled but only display a blank string  dvStr(10,20)
despite having set the value of  dvStr(10,20) = "My Name"

maybe problem with the pointers?



'14:08 26/05/2018
' 2DArray_String.o2bas

'  String 2D array

    $ filename "2DArray_String.exe"
       uses rtl64

class ArrayObjectXYStr
==================
'  this is for creating a string array
  sys pStrA
  int sc,sx,sy
  '
  method constructor(int x,y)
    sc=sizeof(string)
     sx=x : sy=y
    pStrA= getmemory sc*x*y
  end method
  '
  method destructor()
    freememory pStrA
    sc=0 : sx=0 : sy=0
  end method
  '
  method dvStr(int x,y) as string
    x--    :    y--         'BASE 0
    string daStr at pStrA + sc*(x+y*sx)
    return daStr
  end method
  '
  method dvStr(int x,y,string arrStr)
    x--    :    y--          'BASE 0
    string daStr at   pStrA+sc*(x+y*sx)
    daStr =arrStr
  end method
  '
end class


'----------------------------------------
' Main
'  setup a new String array called StrArr
   new  ArrayObjectXYStr   StrArr(100,200)
    StrArr.dvStr(10,20)= "My name"

    print         "    dvStr(10,20)   "  + StrArr.dvStr(10,20)  + chr(10,13) +
                    "    dvStr(11,20)    "  +  StrArr.dvStr(11,20)



del StrArr


Charles Pegge

Hi Chris,

Use bstrings inside the class, since local strings are volatile.

Then all the bstrings in the array must be freed in the destructor. (invoked by del)


'  String 2D array

    $ filename "2DArray_String.exe"
    uses rtl64

class ArrayXYString
===================
  '2D STRING ARRAY
  sys bu        'ARRAY BUFFER
  int sc,sx,sy  'SIZE AND INDICES
  bstring*bs    'BSTRING OVERLAY
  '
  method constructor(int x,y)
    sc=sizeof(string)
    sx=x : sy=y
    bu=getmemory sc*x*y
  end method
  '
  method destructor()
    int i
    sys*p=bu
    for i=1 to sx*sy
      freememory p 'FREE ALL BSTRINGS
      @p+=sc
    next
    freememory bu 'FREE BUFFER
    sc=0 : sx=0 : sy=0
  end method
  '
  method vStr(int x,y) as string
    'GET STRING
    x-- : y-- 'BASE 0
    @bs=bu + sc*(x+y*sx)
    return bs
  end method
  '
  method vStr(int x,y,string s)
    'SET STRING
    x--: y-- 'BASE 0
    @bs=bu+sc*(x+y*sx)
    bs=s
  end method
  '
end class


'----------------------------------------
' Main
'  setup a new String array called StrArr
   new  ArrayXYString   StrArr(100,200)
    StrArr.vStr(10,20)= "My name"
    StrArr.vStr(11,20)= "World"

    print         "    vStr(10,20)   "  + StrArr.vStr(10,20)  + chr(10,13) +
                    "    vStr(11,20)    "  +  StrArr.vStr(11,20)

del StrArr


Chris Chancellor

Thanxx a lot Charles

bstring is a better string ! 

Chris Chancellor

Hello Charles

is bstring  in O2  equivalent to BSTR  of C++ ?

i couldn't find any reference on bstring


Charles Pegge

Yes, o2 recognises bstr and bstring as the same type.

Eduardo Jorge

Chris ,
what are your considerations with arrays, variables, and O2 code structure?
all I do is in with dynamic data and I often use up negative indexes
type
arrayN (-2 to 5, 3 to 10)
I understand it would look something like

LT = total lines = abs ((- 2) -5) +1
CT = total columns = abs (3-10) +1

arrayN (LT * CT)

and for access
L = line =
C = column =
MiL = lowest index line = - (- 2)
MiC = lowest index column = - (3)

arrayN ((L + MiL) * CT + C + MiC)
I'm still testing functionality,

my biggest difficulty is about the structure of the code, I use a bit of public variables so I do not need to be defining parameters for each function
and by what I understand I have to create a pointer with the name of the arrays publishes in the beginning "before the functions that uses them"
and when you have the sizes do a redeim and apply the pointer

Chris Chancellor

Unfortunately Eduardo,  i only use indices from 0 upwards,  i have never encounter
or use arrays with negative indices.  What is the purpose of your usage ?

What fields of work that would use negative indices in arrays? 

Best that you ask Charles on this matter.

Eduardo Jorge

#7
Hello,
it's not about negative index, it's more about code structure
about dynamic index varying between negative and possitive scales I think the account beats
at least in that the code seems to work,

int n0,n1
dim L, C,Lt,Ct,an ,L1,L2,c1,c2
L=3
c=10
redim long Arr2(L*7)
int *Arr1

function  CriaCrray(int  ArrayNx,
int Linha_Lbound,int Linha_Ubound,
int Coluna_Lbound,int Coluna_Ubound)
int n= ArrayNx *7
LT=abs(Linha_Lbound - Linha_Ubound)+1
CT=abs(Coluna_Lbound - Coluna_Ubound)+1

Arr2(n)=getmemory LT*CT*sizeof int

end function

an=1
L1=-2
L2=5
c1=3
c2=10

CriaCrray(an, L1,L2,c1,c2)
@Arr1= Arr2(an*7)

int n=0
for L=L1 to L2
for  c=c1 to c2
Arr1((L+-L1)*Ct+(c+-c1))=n
n=n+1
next
next

L=-1
C=5

print Arr1((L+-L1)*Ct+c+-c1)


3 4 5 6 7 8 9 10

0 ,1 ,2 ,3 ,4 ,5 ,6 ,7,  ' -2
8 ,9 ,10 ,11 ,12 ,13 ,14 ,15,' -1
16 ,17 ,18 ,19 ,20 ,21 ,22 ,23,' 0
24 ,25 ,26 ,27 ,28 ,29 ,30 ,31,' 1
32 ,33 ,34 ,35 ,36 ,37 ,38 ,39,' 2
40 ,41 ,42 ,43 ,44 ,45 ,46 ,47,' 3
48 ,49 ,50 ,51 ,52 ,53 ,54 ,55,' 4
56 ,57 ,58 ,59 ,60 ,61 ,62 ,63}' 5


my problem is that I can not get past this, like for example to use the address array to save the limits of the arrays and to use a macro for the calculation

Eduardo Jorge

I think I found the problem
the array idice does not accept the type of calculation


if do it pampers
   Arr1(L+IBoundNx_M(AN))*TBoundNx_M(AN+1)+(c+IBoundNx_M(AN+1))

have to do this
      B=(L+IBoundNx_M(AN))*TBoundNx_M(AN+1)+(c+IBoundNx_M(AN+1))

Arr1(B)