Sign in to follow this  
Followers 0
jimmyjmmy

Questions on arrays

9 posts in this topic

Hi,

How do I pre-declare an array with an unknown number of elements that it is going to hold. For example, when I read a textfile into an array, the number of elements in this text file is unknown to me and yet I need to pre-declare the array, so how do I pre-declare this array.

In perl it is written as :-

my @array = ();

Thanks

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

I have tried to use your code the other way around:

$iArray = _ArrayGetLength($oFullArray)

    For $i = 1 To $iArray
        FileWriteLine($sFile, $oFullArray[$i])
    Next

and i get the following error:

=> Array variable has incorrect number of subscripts or subscript dimension range exceeded.

i have no idea what else to try :P(

Edited by j00ls

Share this post


Link to post
Share on other sites

Look into Ubound

Share this post


Link to post
Share on other sites

Dim it as an array with only one subscript to start, then just before you go to add another element, use ReDim to resize the array without destroying the previous contents.

...usually it'll be something like redim $iArray[ubound($iArray)+1]


"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110

Share this post


Link to post
Share on other sites

thx i will give redim a try... i found a workaround solution ... but they allways come back to haunt you so I guess il try to fix it the way its supposed to be :P

Share this post


Link to post
Share on other sites

Please pardon me.

So how do I create an empty array.

Thanks

Share this post


Link to post
Share on other sites

Please pardon me.

So how do I create an empty array.

Thanks

Check the help file. Or Try

Dim $aArray[1] <- single-dimensional array with one element numbered zero.

Share this post


Link to post
Share on other sites

Check the help file. Or Try

Dim $aArray[1] <- single-dimensional array with one element numbered zero.

Right...you can't Dim $aArray[0]...to be an array it has to have at least one subscript. And you can't redim a variable to make it into an array. So you could either have a special case for the first value found (like:
if $iArray[0]<> "" OR UBound($iArray)>1 Then Redim $iArray[UBound($iArray)+1]; this will not redim the array if it has only one subscript and that subscript is empty

), or you could assign the value FIRST, always redim it one greater AFTER, and know that you'll always have one "extra" subscript and eliminate it at the end of your loop

Redim $iArray[UBound($iArray)-1]

Hope that helps.


"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0