Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
about arrays
12-22-2017, 12:34 AM
Post: #1
about arrays
When you use an array to hold your variables in a SPLIT or TOKEN function, the array gets resized upwards (apparently never downwards) automagically. But when you run a regular routine like this

DIM a(10)
FOR f - 1 TO 11
a(f) = f
NEXT

You will get an "array out of bounds" error.

So, my question is this: Apparently, the code to have autogrowing arrays is already in there. Why should we not be able to use that in regular array operations? Is it just because way back when yabasic was running on 512K of RAM you might run out of memory? If so, is that thinking still relevant? I fed a GB of data into a string array once for testing purposes. Dog-slow, but yab didn't crash.
Visit this user's website Find all posts by this user
Quote this message in a reply
01-02-2018, 02:16 AM (This post was last modified: 01-02-2018 03:13 AM by bbjimmy.)
Post: #2
RE: about arrays
An easy solution to your problem is this:

DIM a(10)
FOR f = 1 TO 11
dim a (f)
a(f) = f
NEXT

The dim statement in the loop will do nothing unless it is dimensioning the array larger than it is currently.

This can be seen here:

DIM a(10)
FOR f = 1 TO 11
dim a (f)
print "f ",f
print "arraysize(a(),1) ", arraysize(a(),1)
a(f) = f
NEXT

The resizing in the token / split commands works differently. it removes all the data and then demensions the array up/down to match the number of tokens returned and fills the array with the data from the tokens:

dim ws$(1)
a$="this is a test"
num=split(a$,ws$())
print "num ",num
print "arraysize(ws$(),1) ",arraysize(ws$(),1)

a$="this is"
num=split(a$,ws$())
print "num ",num
print "arraysize(ws$(),1) ",arraysize(ws$(),1)

a$=""
num=split(a$,ws$())
print "num ",num
print "arraysize(ws$(),1) ",arraysize(ws$(),1)


This downsizing only works on single demension string arrays.

Fat Elk Sortware
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

Free Web Hosting