How do i get the length of an array?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

1). How do i get the length of an array? e.g. how many elements:

i need something like this:

dim myArray(4)
len(myArray) returns 4?

sorry im going a bit thick, im looking through the docs as i type ::fumble:: :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

Sorry, there's no way to do this.

You need to do it yourself using a Global Variable.

Global ArrayLen.l

ArrayLen = 4
Dim myArray(ArrayLen)

Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> How do i get the length of an array?

Arrays are only the length that the programmer makes them, so why would
you need to get their length if you made them a certain length?


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

i just wanted to know if i could get the length so i could define 'for' loops to process data within dynamically created arrays. i.e. arrays that are created depending on the number of lines within a text file which are always different.
[snip]

Code: Select all

Declare.l countLines(fileNumber.b, fileName.s)
Global maxDebits
maxDebits.l = countLines(#DEBIT_FILE, "debits.ini")/3
Dim debitArray.s(maxDebits,3)
If ReadFile(#DEBIT_FILE, "debits.ini")
    For x=0 To maxDebits
        If Eof(#DEBIT_FILE) = 0
            debitArray(x,0)=ReadString()
            debitArray(x,1)=ReadString()
            debitArray(x,2)=ReadString()
        EndIf
    Next
    CloseFile(#DEBIT_FILE)
EndIf
[/snip]

:)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Have you tried SizeOf() :)
Normally it's for Structures, but maybe it works also with arrays.


Have a nice day...

Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Global maxDebits
> maxDebits.l = countLines(#DEBIT_FILE, "debits.ini")/3
> Dim debitArray.s(maxDebits,3)

I still don't understand... you've got the number of elements above,
stored in the maxDebits variable, so why do you need to get the length
if you already have it? Is it because maxDebits will change the next
time you count the number of lines? If so, just dimming the array
again will auto-resize (and clear) it to that value... which is what
I think you want? Perhaps you didn't know you could re-dim an array
with a new maximum size?


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

oh yeah sorry that code was my little work around but i just wondered if there was a 'lazy' array length function so save a little brainpower as in python :) The #DEBIT_FILE is a dynamically changing file so i will have to re-dim the array. oh well, thanks anyway.

and yeah thanks Franco i tried SizeOF() but it is only for structures :cry:

BTW i am really begining to love PureBasic after recently purchasing it, I'm just adjusting to programming properly, instead of just scripting :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.
Originally posted by Kale

oh yeah sorry that code was my little work around but i just wondered if there was a 'lazy' array length function so save a little brainpower as in python :) The #DEBIT_FILE is a dynamically changing file so i will have to re-dim the array. oh well, thanks anyway.

and yeah thanks Franco i tried SizeOF() but it is only for structures :cry:

BTW i am really begining to love PureBasic after recently purchasing it, I'm just adjusting to programming properly, instead of just scripting :)
Hi Kale, I also redim arrays, sometimes I need this without loosing the data.
What I always do is; if I have an array like:
Dim Array(34)
I use Array(1) till Array(34) for data and Array(0) is ALWAYS the holder for my Array size.
This said, I code always:

Dim Array(34) : Array(0)=34

Maybe this helps.

Have a nice day...

Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

thats sounds like a good idea Franco :) i'll give it a try
Post Reply