• Welcome to Jose's Read Only Forum 2023.
 

About REDIM and DIM, SPANOF

Started by Nicola_Piano, June 16, 2022, 09:25:47 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Nicola_Piano

Hi,
could you tell me where am I wrong in using the REDIM command?
If you look at the example, no errors are thrown when compiling, but the program crashes while running ...
Thank you.


Excuse me .... I solved the problem ...
actually if you want to use the REDIM command you have to initialize the array already with the REDIM command. That is:

REDIM STRING CARI(1)
...
  REDIM STRING CARI(x)
...

in this way I saw that the program does not crash.




'Nicola Piano
'Read lines of a file.txt
'==============================
use console
string mycsvData,ch, temp
string crlf = chr(13)+chr(10)
int i,j,x,y,flen,my,mx
'dim string cari(1) 'IT'S NOT GOOD IF YOU WANT TO USE REDIM ON THIS ARRAY
redim string cari(1) ' THIS IS OK.

mycsvData = getFile("datir.txt")

printl mycsvData

flen = len(mycsvData)
x=1
for i = 1 to flen
  ch = mid(mycsvData,i,1)
    select asc(ch)
    case 13 'Carriage Return o CR
      cari(x)=temp         'set temp value into array at x,y
      x=x+1 'visto il CR devo passare alla riga successiva
      redim string cari(x+1)
      temp="" 'svuoto il buffer dei dati campo
    case 10
'printl 10

    case else
      temp = temp + ch        ' put char into temp

  end select
  mx=x
next i
cari(x)=temp
' end read file ----------------


printl "--------------------"
printl
for i=1 to mx
   printl "[" i "]" cari(i)
next i
printl
printl cari(2)
printl cari(5)
printl "numero di colonne: " my
printl "numero di righe  : " mx

waitkey


and why is it not possible to use a variable to give the dimension to an array?


use console
const int n=10
int m=20
dim string s1(n)
dim string s2(m)  'error ???



why doesn't SPANOF work with REDIM?
int n
dim string s(20)
'redim string v(10)
print spanof s
print spanof v

Charles Pegge

Hi Nicola,

Unlike redim, dim produces non-dynamic arrays, where the number of elements must be known at compile-time to allocate fixed space. You can make m an equate or a const. These are fixed at compile-time.

% m=20

Nicola_Piano

#2
ok, but also with "% m ..." we define a constant that cannot be changed during the execution of the program.
... and SPANOF?

use console
const int n=10
% m=20
dim string s1(n)
printl "n: " n
printl "m: " m

m=m+1
n=n * 2

dim string s2(m)  'error ???

printl "n: " n
printl "m: " m
wait


Zlatko Vid

Just small thing Nicola
please can u use array[n] as square bracket not parens ()
parens are for functions 

Zlatko Vid

i really never have need to use REDIM ...i even don't know that exists  ;D

Charles Pegge

Available memory space is so huge that redim is not required very often. We can afford to dimension arrays generously.

Re: adding values to equates and consts. I'll try to trap such attempts :)

Nicola_Piano

#6
You're right Charles,
but coming from when memory was always a problem ... I still used to.

What can you tell me about using SPANOF on an array created by REDIM?




@Aurel,
I use round brackets because I saw that Charles also uses them ...
Also in the help file,  square and round brackets are used alternatively ...
In this regard, what Charles says, which one should we use as correct notation, square or round brackets?



Charles Pegge


Yes, I remember relying much more on disk storage for large data.

spanof is a constant and only applies to static arrays, so your code needs to remember the redim count.

Zlatko Vid

the most standard ways for arrays is square brackets
parens are for functions ..including built-in functions

myArray[n]  -> array

myFunc(n)  -> function

Charles Pegge

The only situation where square brackets are essential is when you make a C-style array:

int MyInts[100]

This is because it would otherwise be interpreted as a C-style function declaration

int MyInts(int i)

Zlatko Vid

well Charles i always use [] because is easier to follow what is what