Mintoris Forum

Author Topic: Array not dynamically resizing  (Read 4332 times)

robotMeiker

  • Full Member
  • ***
  • Posts: 49
Array not dynamically resizing
« on: Aug 04, 2013, 05:19 AM »
Example from the Mintoris.Basic reference manual:

Dim a(0)

Dim b(10000)

b() = 256

a() = b()

dynamically adjusts the size of a()

MY TEST;

Dim myFileList$(0)

d$() = Dir$()

myFileList$() = d$()

DOES NOT DYNAMICALLY ADJUST THE SIZE OF myFileList$().

What am I doing wrong?

Cesar

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Array not dynamically resizing
« Reply #1 on: Aug 04, 2013, 06:57 PM »
I just tried it and it seems to work fine. What makes you say the size is not correct?

robotMeiker

  • Full Member
  • ***
  • Posts: 49
Re: Array not dynamically resizing
« Reply #2 on: Aug 04, 2013, 07:25 PM »

Actually the d$() is a collection of all the files in my directory, then I pass it thru a filter that extracts all the *.txt files and puts it in a pre defined array, dd$(16), there might be only eight text files put into dd$() which was previously defined to be dd$(16), and when I do:

Dim myFileList$(0)

Dim dd$(16)

d$() = Dir$()

dd$() = myFilterFunction$(d$())

myFileList$() = dd$()

and when I do:

List myFilesList$(), f$

my display shows sixteen choices eight which are real files, and the others are null, which are selectable. The myFilesList$() was not re-dimensioned.

Thanx

Cesar





Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Array not dynamically resizing
« Reply #3 on: Aug 04, 2013, 08:34 PM »
Hmm, I tested all that and it seems to work. My guess is that the problem is in your myFilterFunction$() sub.  Post that sub if you want and I'll take a look at it.

Oh, btw, you don't have to do a dim a(0) any more if you are going to initialize that array in an equate statement.  a() = b()  That dim a(0) doesn't hurt anything though.

so you could condense the top part of your program to just:

myFileList$() = myFilterFunction(Dir$())

List myFilesList$(), f$
« Last Edit: Aug 04, 2013, 08:42 PM by Chuck »

robotMeiker

  • Full Member
  • ***
  • Posts: 49
Re: Array not dynamically resizing
« Reply #4 on: Aug 05, 2013, 12:53 AM »

Thanx Chuck for your help, this is what I'm using in my program to
get the "*.txt" file names and offer the choices. I removed the debug
comments and hope that I didn't remove some of the real statements.
This was a work around since I could not find any other way to be
selective in getting specific file names from the directory path.

I used Basic back in the 80s, but since C/C++, my mind is still thinking
arrays the C/C++ way and creating a mental block when it comes to
Basic Arrays, Since Arrays in Basic are a mystery now and I can only
guess that a Basic Array is actually an Array of structures and in one of the
structure members is an actual array[] with a fixed depth. Since
in C/C++ I can see and get to any byte in a structure or
array and I don't have that accessibility in Basic.

rem ************** get the file name to print ***********************

dim d$(0)

Rem Get a list of files and directories in the root directory.
d$() = Dir$()

Dim fileList$(0)
Dim n(0)

d$() = Dir$()
n() = GetDim(d$())
Dim dd$(8)
forList$ = ""
dd$() = forList$
j = 0
For i = 0 to n(1)
forList$ = d$(i)
If Right$(d$(i),3) = "txt" Then addlist(&j, &forList$, &dd$())
Next i

Print "done dir list";i
fileList$() = dd$()
List fileList$(),f$

rem f$ is file to print
Print "FILE TO PRINT::";f$
Print

END

Sub addlist(j, forList$,  dd$())
dd$(j) = forList$
j = j+1
rem for debug
print j;forList$
End Sub


Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Array not dynamically resizing
« Reply #5 on: Aug 05, 2013, 02:06 AM »
I couldn't quite follow what you were doing so I just wrote a DirFilter$() sub. Give it a try. I've also attached it so you can download it quickly.


d$() = DirFilter$("txt")

list d$(), f$




Sub DirFilter$(ext$)

  d$() = Dir$()
  n() = getDim(d$())

  nbrFiles = 0
  For i = 0 to n(1)
    if lower$(right$(d$(i),3)) = ext$ then nbrFiles = nbrFiles + 1
  next

  if nbrFiles > 0 then
    dim filtered$(nbrFiles-1)
    j = 0
    For i = 0 to n(1)
      if lower$(right$(d$(i),3)) = ext$ then
        filtered$(j) = d$(i)
        j = j + 1
      endif
    next
  else
    ' No files found
    ' return a 1 element array
    ' with a single empty string
    dim filtered$(0)
  endif

  return filtered$()

End Sub

robotMeiker

  • Full Member
  • ***
  • Posts: 49
Re: Array not dynamically resizing
« Reply #6 on: Aug 06, 2013, 05:31 AM »
Thanx Chuck for your help, I just found your reply. I will give it a try and let you know tomorrow. I'm sure that it's going to work just fine. At the start of this part of my project, I was expecting this to work:

d$() = Dir$( "./*.txt")

Thanx

Cesar

robotMeiker

  • Full Member
  • ***
  • Posts: 49
Re: Array not dynamically resizing
« Reply #7 on: Aug 07, 2013, 05:12 AM »
Thanx again Chuck for the "FilterDir.bas" Sub. It worked flawlessly as expected. Very clever to get the count of files first, then allocate the array size. I was being stubborn by expecting that I could do it by dynamic resizing of the output array. The Sub was very helpful but more than that, it was a bucket full of lessons. Even though I've seen many of the other Basic progs that others have contributed, this time your Sub has added more clarity to Mintoris.Basic programming. For this application I'm using Mintoris.Basic to control via Bluetooth my design for a 3D printer. So far the printer is mechanically working as designed. But have not as yet made anything with it.

Cesar