www.ClassicTW.com
https://mail.black-squirrel.com/

array size
https://mail.black-squirrel.com/viewtopic.php?f=15&t=23563
Page 1 of 1

Author:  Hotblack Desiato [ Fri Oct 02, 2009 12:38 pm ]
Post subject:  array size

Having problems with a script... after pulling my hair a little, turns out that my array size is zero. So I write a little test script:

Code:
#test.ts
setArray $fig_output 8
echo "** fig_output array size: "& $fig_output & "**"


output:
--------------------------------------------------------------
Loading and compiling script: scripts\testfig.ts

Command [TL=00:00:00]:[77] (?=Help)? :

fig_output array size: 0

Script terminated: scripts\testfig.ts
--------------------------------------------------------------

Wtf????

Author:  Hotblack Desiato [ Fri Oct 02, 2009 1:49 pm ]
Post subject:  Re: array vars

Okay... seriously....

Code:
#testArraySize.ts
setArray $figArray 2
setVar $figArray[1] "one"
setVar $figArray[2] "two"
echo "*" $figArray[1] "*"
echo "*" $figArray[2] "*"
echo "** figArray size: " $figArray "**"
halt


------------------ output ---------------------------------------
Loading and compiling script: scripts\testArraySize.ts

Citadel command (?=help)
one

two


figArray size: 0


Script terminated: scripts\testArraySize.ts
------------------ /output ---------------------------------------

Author:  Hotblack Desiato [ Fri Oct 02, 2009 2:33 pm ]
Post subject:  Re: array size

Help!!

Author:  Maniac [ Fri Oct 02, 2009 4:26 pm ]
Post subject:  Re: array size

From looking over the old source code I can't see anywhere EP sets the arrayname = count of array except in his new routines
I.E. and I have noticed that the documentation is incorrect

in the documentation of GetCourse

From Example of GetCourse

getCourse $course 1 10

Would return:

$course = 2
$course[1] = 1
$course[2] = 2
$course[3] = 10

The variable course is an array with length 3 so $course should be set to 3 instead it is set to 2

I have found it in the source code and the documentation is correct

Around line 384 in function GetCourse in file ScriptCmd.pas
try
Params[0].Value := IntToStr(Course.Count - 1);

This is documented from Xide I believe but Elder has fixed some but not all
Array==LengthOfArray

Nice catch of that bug.... Undocumented Feature I mean
Heh it's the old One off error.... arrays in most languages start at 0 while most humans use 1


There are no bugs in TWXProxy :)

Author:  Hotblack Desiato [ Fri Oct 02, 2009 4:42 pm ]
Post subject:  Re: array size

Thanks Maniac, this is driving me nuts...

but I'm not following... how do i fix it? It's not just one-off for me, it's giving array size 0 no matter what.

I don't get it... I'm sure I've used array size before and it's worked fine.

Author:  Maniac [ Fri Oct 02, 2009 5:39 pm ]
Post subject:  Re: array size

From more investigating is is only on Static arrays that don't get the Array[0]=SizeofArray
I looked at GetNearestWarps,FileList,and a couple of other routines that
return Arrays and I see that they work as advertised.


There are a couple more areas I am unsure of such as GetCourse tries to return the # of steps from a to b
but forgets you have one extra step in there

GetCourse 1 10 will return 2 when there are 3 elements i.3. 1.2.10
I think this is because we also use GetDistance which is 2... 1->2 is 1 and 2->10 is 2 so the distance between 1-10 is 2 hops


Maybe EP will chime in on this and let us know.

Author:  Singularity [ Fri Oct 02, 2009 6:31 pm ]
Post subject:  Re: array size

Not that you need it, when you use setArray you have to know the size somewhere anyway... ;)

It used to never report the size, that was a problem for internal algos that generated arrays since you'd have to count them. EP
added the convention where $var = sizeof($var[]) for returned arrays. But since $var and $var[] are 2 different entities, it's only
true of places where $var[] is populated internally by a twxproxy command. It would be nice to have a sizeof() command tho.

This isn't really a bug. And it shouldn't be changed, it'll break scripts if you do. Some of us are used to $var and $var[] being
completely separate, and we use them as such. If you need $var to report sizeof($var[]) in your script then init it at the top and
just "add $var 1" along the way.

IMO, $var should not automatically report sizeof($var[]) for every single array. It would be useful tho to have a SizeOf or ArraySize
command for times where it's needed.

Author:  Hotblack Desiato [ Fri Oct 02, 2009 6:35 pm ]
Post subject:  Re: array size

Thanks for explaining that. Weird. I agree there should just be a sizeof command or operator (or whatever people want to call it).

Quote:
Not that you need it, when you use setArray you have to know the size somewhere anyway... ;)


Strange, but I hadn't thought of that. I guess oop has brainwashed me over the years - the object should know how big it is, but of course this isn't oop.

Author:  Singularity [ Fri Oct 02, 2009 7:13 pm ]
Post subject:  Re: array size

hehe. Yeh, from an OOP perspective it's all weird. OOP makes a lot of assumptions.

Author:  ElderProphet [ Fri Oct 02, 2009 9:46 pm ]
Post subject:  Re: array size

All of the behavior mentioned is intentional. All of the commands that create a size variable with the same name as the array (getAllCourses, getCourse, getNearestWarps, listActiveScripts, listAvoids, listSectorParameters, readToArray) document this functionality, all added by me except getCourse, and all actually specify the size except for the 2 course commands. The behavior of the getCourse command was logical to Xide, and it made sense for me to mimic this established behavior with the getAllCourses command.

Despite the above commands, TWX does not track the size of arrays in a way that is available to a script, nor is there any general function to determine the size of an array. And I agree that it is sorely needed.

I wrote about some of this in an earlier thread, which may clarify some of it for you: http://www.classictw.com/viewtopic.php?p=189560#p189560

And if I can clarify further, let me know.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/