Page 1 of 1 1
Topic Options
#75226 - 2003-05-30 07:57 PM array / dir bug
Christophe- Offline
Fresh Scripter

Registered: 2003-04-28
Posts: 5
Hi,
This little script illustrates the problem.
In my script I need to use the dir command after the array declaration.

Regards, Christophe.

$path="C:\"

;count the number of files in the dir
$c = 0
$f = Dir("$path\*.txt")
While @error = 0
$c = $c + 1
$f = Dir()
Loop

;declare the array
Global $array[$c,1]

;check the array size
? "-1-"
? Ubound($array,1)
? Ubound($array,2)

;check insert into array
$array[0,0]="test"

;this is the command that screws up my array !
$FileName = Dir("$path\*.txt")

;Check the array size (at execution dimension 2 size is wrong)
? "-2-"
? Ubound($array,1)
? Ubound($array,2)

;Check insert into array (at execution time I have an out of bound error)
$array[0,0]="test"

[ 30. May 2003, 22:20: Message edited by: Christophe- ]

Top
#75227 - 2003-05-30 08:52 PM Re: array / dir bug
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Before you reinvent the wheel, take a look at any of the DIR...() UDFs, like DIRPLUS() and DIRLIST(). They are posted in the UDF Forum.
_________________________
There are two types of vessels, submarines and targets.

Top
#75228 - 2003-05-30 09:15 PM Re: array / dir bug
Christophe- Offline
Fresh Scripter

Registered: 2003-04-28
Posts: 5
Hi,
Maybe I was not clear enough.
My problem is not how to deal with dirlist or dirplus but the fact that using the dir command is ruining the array I use in the same script.

May someone please run this script and find if they have the same error I have commented here ?

Top
#75229 - 2003-05-30 09:27 PM Re: array / dir bug
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Running your script results in
code:
-1-
0
1
-2-
0
1

I don't see anythign wrong with it. However, you never set $x to any value. alos if you want to just get all text files into an array then you can dynamically resize the array with REDIM PRESERVE and you don't have to enumerate the files twice.
_________________________
There are two types of vessels, submarines and targets.

Top
#75230 - 2003-05-30 10:19 PM Re: array / dir bug
Christophe- Offline
Fresh Scripter

Registered: 2003-04-28
Posts: 5
Ok for the $x. I did a quick copy and paste from my main script and I forgot a part of it. [I corrected it it the main post]
Anyway this is not important for the test.

Here is my output:
-1-
10
1
-2-
10
193

I also have the error: array reference out of bounds.

If I remove the $FileName = Dir("$path\*.txt") I obtain:

-1-
10
1
-2-
10
1

And I can write in the array without problem.

[ 30. May 2003, 22:21: Message edited by: Christophe- ]

Top
#75231 - 2003-05-31 01:11 AM Re: array / dir bug
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Put DEBUG ON into the first line of your code and step through the code. This way you can evaluate variable contents while running the script. However, I think there's something wrong with your code, but without the complete code it's hard to tell.

[ 31. May 2003, 01:11: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#75232 - 2003-06-02 09:58 AM Re: array / dir bug
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You don't issue a newline after displaying the Ubound() values, so my guess is that the "193" you are seeing is not all from the Ubound(), but rather the "1" from Ubound() followed by output from a function later in your script.

It just looks like it is incorrect because the values are catenated.

Top
#75233 - 2003-06-02 02:44 PM Re: array / dir bug
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Good catch, Richard.
Christophe-, please read the FAQ Forum, especially Why does the console display zeros and ones (amongst others)?

[ 02. June 2003, 16:48: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#75234 - 2003-06-02 04:39 PM Re: array / dir bug
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
I don't know. I'm getting the same results as Christophe. And my entire script is just the snippet of his from above.

First off though, Christophe should either set $path = "c:" or change is dir calls to "$path*.txt", because right now it's doing a dir on "c:\\*.txt".

And, with the little testing I have done, if the last dir call is changed to just dir(), it works fine. I'll look at it some more.

I'm using kix 4.21 on win XP.
_________________________
Eric

Top
#75235 - 2003-06-02 04:43 PM Re: array / dir bug
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Alright, that seemed a little to easy. Christophe, try putting your global declaration at the top of your script and not in the middle. That seems to correct the problem for me. Let us know if it works.
_________________________
Eric

Top
#75236 - 2003-06-02 05:10 PM Re: array / dir bug
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I take it all back. I'd definately report this as a bug, Christophe.

It's really weird.

Simplest I can make the code is:
code:
$f = Dir("*",0)
While @error = 0
$f = Dir(,0)
Loop

;declare the array
Global $array[10,99]

;check the array size
"[1] Array ubound should be 99: " Ubound($array,2) ?
$f = Dir("*",0)
"[2] Array ubound should be 99: " Ubound($array,2) ?

This fails with 4.20.

Interestingly,
  • If you use index 0 with the first Dir() and index 1 with the second you don't get the problem.
  • If you don't exhaust the first loop you don't get the problem.
  • If you have more than 2 array dimensions you don't get the problem.
{EDIT}
Update - Fails with latest 4.21 too.
{/EDIT}

[ 02. June 2003, 17:17: Message edited by: Richard H. ]

Top
#75237 - 2003-06-02 05:29 PM Re: array / dir bug
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Richard, did you try moving the global declaration to the beginning of the script?

-Eric
_________________________
Eric

Top
#75238 - 2003-06-02 05:38 PM Re: array / dir bug
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Looks like a DIR() problem whne restarting the DIR enumeration with the same enumeration handle as this works:
code:
break on

$f = Dir("*",0)
$a=0
While @error=0
$f = Dir(,0)
Loop
;declare the array
Global $array[10,99]
;check the array size
"[1] Array ubound should be 99: " Ubound($array,2) ?
$f = Dir("*",1)
"[2] Array ubound should be 99: " Ubound($array,2) ?

_________________________
There are two types of vessels, submarines and targets.

Top
#75239 - 2003-06-02 09:03 PM Re: array / dir bug
Christophe- Offline
Fresh Scripter

Registered: 2003-04-28
Posts: 5
Hi Guys,
Thank you for the great support. [Smile]
I have found a way to go around the problem (declaring the array at the start of the script and making a function to count the number of files).
However I think that there is a bug as some people could reproduce the problem.
I hope someone will find an explanation or a fix for the other users that may encounter the same issue.

I have to post a new message as now I have another problem [Confused]
[edit] I think I won't need to post as I'm in my way to find a solution to my problem [Smile]

[ 02. June 2003, 21:36: Message edited by: Christophe- ]

Top
#75240 - 2003-06-03 09:31 AM Re: array / dir bug
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
quote:
Richard, did you try moving the global declaration to the beginning of the script?
No, because I was determining whether or not there is a bug in place rather than a way of avoiding it.

It appears that there is, and it may be significant as it implies that there is uninitialised memory used somewhere.

It needs to be reported as a bug as it may impact more than simply global variable declaration.

quote:
Looks like a DIR() problem whne restarting the DIR enumeration with the same enumeration handle as this works:
Yes, see the first bullet point in my last post [Wink]

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 884 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.065 seconds in which 0.022 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org