Page 1 of 1 1
Topic Options
#79294 - 2002-11-08 03:04 PM FOR EACH behavior
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Folks:

Please run the script below and comment on the bahavior. This behavior is not pertinent to KiXtart 4.12 RC-1 but seems to be a more general problem.
code:
break on
cls

;segment 1
dim $array[2]
$array[0]='Array Entry 0'
$array[1]='Array Entry 1'
$array[2]='Array Entry 2'
for each $element in $array
? 'Element = '+$element
next

; segment 2
dim $array
$array='String Entry 0'
for each $element in $array
? 'Element = '+$element
next

I think that the result of segment 2 is incorrect. It should either raise an error because the variable $array is of type string and not type array since the documentation states it has to be an array or collection. Alternatively, or better, preferably, it should treat the string type as an pseudo 1-element array and proccess it accordingly, thus resulting in an output of 'Element = String Entry 0'.
_________________________
There are two types of vessels, submarines and targets.

Top
#79295 - 2002-11-08 03:37 PM Re: FOR EACH behavior
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Yeah, it doesn't sqwak and maybe it should. I think a better behavior would be to treat the string as a collection of chars (which it is) and be able to iterate over each character.
Top
#79296 - 2002-11-08 03:42 PM Re: FOR EACH behavior
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Or modify the SPLIT function to convert a string into an array of chars.
_________________________
There are two types of vessels, submarines and targets.

Top
#79297 - 2002-11-08 04:01 PM Re: FOR EACH behavior
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
The "for each" does tell you it failed ... if you checked. [Razz] And I requested that SPLIT("string","") return an array of single letters some time ago.
code:
break on
cls

;segment 1
dim $array[2]
$array[0]='Array Entry 0'
$array[1]='Array Entry 1'
$array[2]='Array Entry 2'
for each $element in $array
? 'Element = '+$element
next
? "@error @serror"

; segment 2
dim $array
$array='String Entry 0'
for each $element in $array
? 'Element = '+$element
next
? "@error @serror"




[ 08. November 2002, 16:08: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#79298 - 2002-11-08 04:32 PM Re: FOR EACH behavior
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
but @error & @serror are not reset to zero when "For each" executes correctly. If there is not an intervening function that sets @Error to zero the same "-1 Expected an object reference. [-1/FFFFFFFF]" error is repeated.

[ 08. November 2002, 16:33: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#79299 - 2002-11-11 08:50 AM Re: FOR EACH behavior
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I wonder, why you boys do a double dim?
isn't that redimming incorrectly?
shouldn't:
dim $array
$array='String Entry 0'

be:
redim $array
$array='String Entry 0'

or just:
$array='String Entry 0'
_________________________
!

download KiXnet

Top
#79300 - 2002-11-11 01:52 PM Re: FOR EACH behavior
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Lonkero, for me it was just laziness. The ease of copy/paste without fear of trouble for this little test.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#79301 - 2005-10-13 05:47 PM Re: FOR EACH behavior
Doofus Offline
Lurker

Registered: 2005-10-13
Posts: 1
My problem is that even if I DIM the array (DIM $array[10]), if I populate it with only one entry ($array = "element1"), I still encounter the same problems.

The only good workaround I've come up with is to tag a blank element onto the array ($array = "element1", ""), then wrap the contents of the FOR EACH with an IF:

Code:
FOR EACH $element IN $array
IF $element
? $element
ENDIF
NEXT



Which is pretty ugly and a hassle.....

Top
#79302 - 2005-10-13 07:22 PM Re: FOR EACH behavior
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
1.. this is the beta forum
2.. this is a very very old thread! necroposting = bad
3.. when using $array = "element1" $array is no longer a array, it is a string $array = split("element1") will get you are looking for.

Top
Page 1 of 1 1


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

Who's Online
1 registered (Allen) and 675 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

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