September 20, 2020, 04:50:37 am

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


ReDIM how?

Started by aurelCB, January 05, 2010, 01:17:55 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

One thing bugging me all the time.
When i want translate some code from some othere basic like languages like Free Basic to Creative or Emergance i
always stuck with this command REDIM(alias RE DEFINE).
Is there a way to imitate this command?
Is this posibile with CLEAR variable then DEF again, i mean i first place on arrays...

any ideas....

all best
Aurel
forum for hobby programming:<br />https://aurelsoft.ucoz.com

Brian

aurelCB,

Paul showed me how to do it in IB Pro one time. I suppose it should work
the same for Creative. Although I can't remember which program it was
that I wrote that needed it, so I will have to dig it out

Later,

Brian

mrainey

Here are three redim examples I've saved through the years - author(s) unknown.
Software For Metalworking
http://closetolerancesoftware.com

aurelCB

Thanks Mike.... :)
I think that will be very usefull.
By the way little bit tricky is this thing.In FB this things work like here DEF but of course internaly is
probably made et in similiar way.
Ok Brian if you find something just post here.. :)
forum for hobby programming:<br />https://aurelsoft.ucoz.com

aurelCB

Ok here is code with command CLEAR.It is not real REDIM but work similiar
and present simpliest way i think.So this is just a start...

Code Select
'ReDIM 1

Autodefine "OFF"

OPENCONSOLE
def count:INT
count=5
def mystrings[count]:string

'create an array with 5 strings

mystrings[0]="a"
mystrings[1]="b"
mystrings[2]="c"
mystrings[3]="d"
mystrings[4]="e"

'Print the content of the array
FOR n=0 TO (count-1)
   PRINT mystrings[n]
NEXT n
PRINT ""

PRINT "Press any key to clear old array..."
DO:UNTIL INKEY$<>""
CLS

'clear string variables
CLEAR mystrings

'create new array with 10 strings
count=10
def mystrings[count]:string
mystrings[0]="a"
mystrings[1]="b"
mystrings[2]="c"
mystrings[3]="d"
mystrings[4]="e"
mystrings[5]="f"
mystrings[6]="g"
mystrings[7]="h"
mystrings[8]="i"
mystrings[9]="j"

'Print the content of the new array
FOR n=0 TO (count-1)
   PRINT mystrings[n]
NEXT n
PRINT ""

PRINT "Press any key to quit"
DO:UNTIL INKEY$<>""
CLOSECONSOLE
END
forum for hobby programming:<br />https://aurelsoft.ucoz.com

aurelCB

Here is example from Mike which i modify with INPUT command that you can make your tests.
This aproach i think work fine...

Code Select
' EXAMPLE THREE  ReDIM 3
'You can use 'memory' to act as a re-dim'able array (see example below)
'And it's not limited to integers - strings or UDT's will work ok as well (with appropriate modifications). Hardly any performance hit either.
'In general it's not a good idea to start of with say, 10 spaces and changing up by one each time, you need more space. You will soon get a bad case of memory fragmentation (a long story, one I don't know that well....) - also in the speed stakes not a good idea.
'Again it really depends on what you will be using a re-diming facility for to suggest a good overal scheme.
'Also, see user guide for more info on memory.
'Code:

def rtn:STRING


def mem:MEMORY
def temp:INT
def v:INT
def count:INT
def size:INT

Openconsole
size=len(v)

INPUT "Set count(DIM): ",count :'10
'Allocates 10 memory slots of size integer
Allocmem mem,count,size

For temp=1 To count
  v=temp*2
  'writes 'v' into memory slot 't'
  Writemem mem,temp,v
Next temp
'----------------------------------------------------
'Print temp 11
Print "Memory written"
PRINT "Press ENTER and set new size of array..."
DO:UNTIL INKEY$<>""
'---------------------------------------------------
INPUT "New size(REDIM): ",count
'Now reallocates the original to 20 slots
Allocmem mem,count,size
'For temp=11 to 20
For temp=temp to count
  v=temp*2
  'writes 'v' into memory slot 't'
  Writemem mem,temp,v
Next temp
'-------------------------------------------------
Print "Memory written with new size"
PRINT "Press ENTER and see new result of array..."
DO:UNTIL INKEY$<>""
'------------------------------------------------
'temp=1
For temp=1 to count
  'reads into 'v' contents of memory slot 't'
  Readmem mem,temp,v
  Print temp,v
Next temp

'frees/deletes the memory at 'm'
Freemem mem

Input "Press enter:",rtn
Closeconsole



forum for hobby programming:<br />https://aurelsoft.ucoz.com

Brian

Aurel,

Sorry, I can't find my example. I know it was a program I wrote for my
previous employer, so it must have been sitting on the PC when I left

Brian

aurelCB

Dont worry Brian ;)
No problems i will mix something... ;D
forum for hobby programming:<br />https://aurelsoft.ucoz.com

danbaron

I tried to do the redimension in a subroutine, and I could not get it to work
correctly. Below is the code for two EB console programs. They both are written
to do the same thing. The first program uses pointers, and the second uses the
memory commands. When I run the programs on my machine, they both give me the
same result. After the subroutine is called, the first two "array" values are
corrupted.

DTB.


Program using pointers:
Code Select

'------------------------------------------------------------

POINTER P
INT I
INT SIZE = 10
INT NEWSIZE = 20

P = NEW(INT, SIZE)

FOR I = 0 TO SIZE - 1
#<INT>P[I] = I + 1
NEXT I

FOR I = 0 TO SIZE - 1
PRINT #<INT>P[I]
NEXT I

PRINT

REDIM(P, SIZE, NEWSIZE)

FOR I = SIZE TO NEWSIZE - 1
#<INT>P[I] = I + 1
NEXT I

FOR I = 0 TO NEWSIZE - 1
PRINT #<INT>P[I]
NEXT I

INPUT I
END

'------------------------------------------------------------

SUB REDIM(POINTER P, INT OLDSIZE, INT NEWSIZE)
INT I
POINTER P1 = NEW(INT, NEWSIZE)

FOR I = 0 TO SIZE - 1
#<INT>P1[I] = #<INT>P[I]
NEXT I

DELETE(P)

P = P1
ENDSUB

'------------------------------------------------------------


Program using memory commands:
Code Select

'------------------------------------------------------------

INT I, J
MEMORY MEM
INT COUNT = 10
INT NEWCOUNT = 20
INT LENGTH = LEN(LENGTH)

ALLOCMEM MEM, LENGTH, COUNT

FOR I = 1 TO COUNT
WRITEMEM MEM, I, I
NEXT I

FOR I = 1 TO COUNT
READMEM MEM, I, J
PRINT J
NEXT I

PRINT

REALLOCMEM(MEM, LENGTH, COUNT, NEWCOUNT)

FOR I = COUNT + 1 TO NEWCOUNT
WRITEMEM MEM, I, I
NEXT I

FOR I = 1 TO NEWCOUNT
READMEM MEM, I, J
PRINT J
NEXT I

INPUT I
END

'------------------------------------------------------------

SUB REALLOCMEM(MEMORY MEM, INT LENGTH, INT OLDSIZE, INT NEWSIZE)
INT I, J
MEMORY TMEM
ALLOCMEM TMEM, LENGTH, NEWSIZE

FOR I = 1 TO OLDSIZE
READMEM MEM, I, J
WRITEMEM TMEM, I, J
NEXT I

FREEMEM MEM

ALLOCMEM MEM, LENGTH, NEWSIZE

FOR I = 1 TO OLDSIZE
READMEM TMEM, I, J
WRITEMEM MEM, I, J
NEXT I

FREEMEM TMEM
ENDSUB

'------------------------------------------------------------


"You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump."  -  W.C. Fields