Author Topic: Problem with arrays [Solved]  (Read 8528 times)

I have made a function which puts the contents of an array into a string:

Code: [Select]
function arrayToStr(%ar)
{
%str = "";
for (%i = 0; %i < %ar.length; %i++)
{
%str = %str @ %ar[%i];
}
return %str;
}

After that code is executed, when I type this into the console:
Code: [Select]
$testAr[0] = "bl";
$testAr[1] = "ah";
echo(arrayToStr($testAr));
...it echoes a blank string. What's wrong with my script?
« Last Edit: May 21, 2014, 10:47:09 PM by Hammereditor5 »

There are no arrays in TorqueScript. If this was any other language that had strict typing, and %ar was a string[], then there would be no issue.

%ar.length is not a valid variable. Since you can only use pseudoarrays in T'S you'll either need a separate variable for "array" length or you'll need to do a while loop that checks until the array has no more values

The most fundamental flaw here is that you expect these to be represented as objects.

Code: [Select]
$testAr[0] = "bl";
$testAr[1] = "ah";
echo(arrayToStr($testAr));

At this point, $testAr is not defined. $testAr0 and $testAr1 are, though.
Therefore, you're calling arrayToStr with an empty string.

Code: [Select]
function arrayToStr(%ar)
{
%str = "";
for (%i = 0; %i < %ar.length; %i++)
{
%str = %str @ %ar[%i];
}
return %str;
}

Any attribute on an object that does not exist (i.e. an empty string) is also an empty string. Empty strings are treated as 0, and thus that code boils down to:

Code: [Select]
function arrayToStr(%ar)
{
%str = "";
%i = 0;
return %str;
}



Shameless self-promotion: http://github.com/portify/types (see lib/Array.cs)
« Last Edit: May 18, 2014, 10:55:25 AM by portify »

you'll either need a separate variable for "array" length
Thank you. I've noticed I am starting to treat Torquescript like Java.

Thank you. I've noticed I am starting to treat Torquescript like Java.

With the approach you seem to be taking you'll run into variable scoping issues. The only way to create a function that does what you're trying to do is by passing it an object or the name of a global variable.

The only way to create a function that does what you're trying to do is by passing it an object or the name of a global variable.
That starts with a specific character mind you, you can't just go $[%varname][%x]

$endChar = "_._.";

function arrayToString(%array) {
   %i = 0;
   while($String["::" @ array @ %i] !$= $endChar) {
      %output += $String["::" @ array @ %i];
      %i++;
   }
   return %output;
}

$String::test[0] = "abc";
$String::test[1] = "def";
$String::test[2] = "_._.";
echo(arrayToString("test"));



Wouldn't call it the optimal, and I definitely recommend using port's code.
I also haven't tested it.




« Last Edit: May 21, 2014, 12:12:55 AM by Electrk Boogaloo »

Take it to the drama board guys or please stay on topic.

Take it to the drama board guys or please stay on topic.
Sorry, I should have indicated that the problem is solved, so there's no meaningless discussion in here.