May 03, 2024, 11:53:30 PM

News:

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


Arrays - Dynamic and Types

Started by pm620wh, September 16, 2009, 11:34:51 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pm620wh

Hello again

I've been looking to see how you can dimension arrays on the fly - I'm not sure if I found some postings (I guess I'm not clever enough to fully understand what was being said).  I am used to Visual Studio and VB.NET and re-dimensioning arrays like so:

Dim myArray(1)
Dim intCount as Integer
...
For intCount = 1 to someNumber
    ....
    redim preserve myArray(uBound(myArray) + 1)
    ....
Next intCount

Is there a similar - simple way to achieve the same thing in Emergence Basic please?

Plus (and I partly tried this in code but may have made a mistake) is it possible to dimension Arrays as a User Defined Type and then dynamically expand the array, referencing each element of the type as you go e.g.

Dim intCount as Integer
Type myType
   Dim strTemp as String
   Dim intTemp as Integer
EndType

Dim myArray(1) as myType

For intCount = 0 to someNumber -1
   myArray(intCount).strTemp = "Entry Number " + intCount
   myArray(intCount).intTemp = intCount

   redim preserve myArray(uBound(myArray) + 1)

   ....
Next intCount

Many thanks and kind regards

Paul

LarryMc

EBasic does not have a REDIM command.

Sounds like you may need to use Linked Lists (covered in the help file and there is also a sample program)

The nice thing about linked list is that it grows as you need it to grow and if you delete an element it shrinks as needed.
The variable can be of any type including UDTs.

So you can use the LL commands available with EBasic.

Or, you can download my LL class that has a few more commands, a help file, and examples of using with UDTs.
The download is the the User Offering section.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

mrainey

I found something on the forum CD that Fletchie cooked up.  All comments are his.




Ok heres the new version of 'renew'.

Resizes memory allocated via new().

Use:

Renew(p,elements)

Where:

p is the original pointer
elements is the number of new elements

Now optionally for int64s/doubles/strings you must pass the size of that particular data-type as the third parameter. 8 for int64 or doubles and 255 for strings.

The return is True if ok, False if not (original preserved)

Paul, looks like deleting a pointer is interfering with autotyping.

Ian.


declare "kernel32",GlobalSize(hMem:int),int
declare "kernel32",GlobalReAlloc(hMem:int,dwBytes:int,uFlags:int),int
const GMEM_ZEROINIT=0x0040
const GMEM_MOVEABLE=0x2

def p:pointer

'10 'ints'
p=new(int,10)

for t=0 to 9
  #p[t]=t
next t

'Now make it 20 'ints'
'Fudge: The '4' has to be a mem-length of the data-type, 4 for ints,2 for word etc...
'ReNew preserves original (unless we're reducing, then it gets cut off)

if ReNew(p,20)=0
  Print "Failed"
'If the next line is uncommented I get a compile error a few lines down #p[t]=t
'   delete p
  do:until inkey$<>""
  end
endif

'Now we can access 10 to 19.....

for t=10 to 19
  #p[t]=t
next t

'Print them....

for t=0 to 19
  print #p[t]
next t

print

if ReNew(p,2)=0
  print "error"
  delete p
  do:until inkey$<>""
  end
endif

for t=0 to 1
  print #p[t]
next t

for t=1 to 10000
  if ReNew(p,rand(10,50000))=0
     print "Whoops"
     do:until inkey$<>""
     delete p
     end
  endif
next t

if ReNew(p,2^29)=0
  print "Luckly it failed"
endif

delete p

do:until inkey$<>""
end



sub ReNew(pnt:int byref,elements:int,opt elesize=4:int),int

def p:int

  'Total memory size is elesize*elements
  p=GlobalReAlloc(pnt,elesize*elements,GMEM_MOVEABLE|GMEM_ZEROINIT)

  if p=0
     return 0
  else
     pnt=p
     return 1
  endif

endsub
Software For Metalworking
http://closetolerancesoftware.com

LarryMc

Way to go Mike.

I remember seeing that before now that you showed it.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

pm620wh

Thank you both for taking the time to reply.  The second posting from Mike I found myself and thought "hmmmmm". 

Larry, I really like the idea of the linked lists.  You mentioned they can be downloaded, would that be from here?

Many thanks again

Quote from: pm620wh on September 16, 2009, 11:34:51 AM
Hello again

I've been looking to see how you can dimension arrays on the fly - I'm not sure if I found some postings (I guess I'm not clever enough to fully understand what was being said).  I am used to Visual Studio and VB.NET and re-dimensioning arrays like so:

Dim myArray(1)
Dim intCount as Integer
...
For intCount = 1 to someNumber
    ....
    redim preserve myArray(uBound(myArray) + 1)
    ....
Next intCount

Is there a similar - simple way to achieve the same thing in Emergence Basic please?

Plus (and I partly tried this in code but may have made a mistake) is it possible to dimension Arrays as a User Defined Type and then dynamically expand the array, referencing each element of the type as you go e.g.

Dim intCount as Integer
Type myType
   Dim strTemp as String
   Dim intTemp as Integer
EndType

Dim myArray(1) as myType

For intCount = 0 to someNumber -1
   myArray(intCount).strTemp = "Entry Number " + intCount
   myArray(intCount).intTemp = intCount

   redim preserve myArray(uBound(myArray) + 1)

   ....
Next intCount

Many thanks and kind regards

Paul

LarryMc

You can you the Linked list functions that are an integral part of EBasic without downloading anything.
You can find information in the EB help file under Languages/Using Linked List

I was playing with pointers and classes and created a Linked List class that can be downloaded at
http://www.ionicwind.com/forums/index.php/topic,2613.msg22185.html#msg22185

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library