View Full Version : tab delimted strings as an "array"
Halide
09-16-2002, 05:20 PM
ok, this is Tribes 2 scripting (Torque Game Engine) first off.. and it's very similar to PHP
anyways
since it will not return arrays, I want to append values in a string using tab
like this code here
function HalKillSound::getFileList(%this, %search)
{
for(%fileName = findFirstFile(%search); %fileName !$= ""; %fileName = findNextFile(%search))
%fileList @= %fileName @ "\t";
return %fileList;
}
using code like this would result in "file1\tfile2\tfile3\t"
I want to be able trim the trailing \t but using a getSubStr type function seems lame to me
any ideas
there's no way to unilaterally trim the last character from a string? I mean, as long as you have one file in the list, you know that the last character will be a \t.
Halide
09-16-2002, 06:10 PM
i guess i could trim off last char is that the best method
%string = getSubStr(%string, 0, strLen(%string) - 1);
well, I don't know enough about that language to be of much more help. In other languages, there would be a multitude of ways to do it.
Halide
09-16-2002, 07:21 PM
well, that would return the string without the last char... but I just thought it would be "cleaner" to prevent it from attaching the last \t... except I can't figure out a "clean" way to do it :)
oh well... not worth spending too long on
Grizzly
09-17-2002, 08:05 PM
Yeah, I don't know enough about the language either, but would this work?
function HalKillSound::getFileList(%this, %search)
{
for(%fileName = findFirstFile(%search); %fileName !$= ""; )
{
%fileList @= %fileName;
%fileName = findNextFile(%search);
if(%fileName !$= "") %fileList @= "\t";
}
return %fileList;
}
This makes a few assumptions:
For starters, I'm guessing for loop parameters are optional (like in C). For this example, I've elected to remove the last for loop parameter - which should be the process that execs at the end of each iteration through the loop. Instead, I'm explicitely doing that action within the loop structure, so I can evaluate the length of the "next" fileName and append a TAB if appropriate. (So I've basically bastardized it into a WHILE structure)
The reason I removed it as a for loop paramter, is because it *appears* that the function "findNextFile()" increments an internal pointer in the fileList. So doing it once within the loop, and secondly as the 3rd for-loop parameter, would result in skipping every other file.
...so if the language does in fact support a WHILE structure, this would be the WHILE version of my approach:
function HalKillSound::getFileList(%this, %search)
{
%fileName = findFirstFile(%search);
while( %fileName !$= "" )
{
%fileList @= %fileName;
%fileName = findNextFile(%search);
if(%fileName !$= "") %fileList @= "\t";
}
return %fileList;
}
But like I said, I have noooooo clue on the Tribes 2 scripting language. For all I know my code would bomb miserably. Good luck with it though.
TheLinuxDuck
09-18-2002, 10:05 AM
Grizzly, I'm just being *really* nitpicky here.... (= The only thing about the final version that I don't like is the fact that the code is doing the same thing twice, sorta, by checking %filename !$= "" in the while and also at the end (even though they are different. I would prolly put the tab append first in the while loop and check it against %fileList instead, so that the same test isn't being done twice.
function HalKillSound::getFileList(%this, %search)
{
%fileName = findFirstFile(%search);
while( %fileName !$= "" ) {
if(%fileList) !$= "") %fileList @= "\t";
%fileList @= %fileName;
%fileName = findNextFile(%search);
}
return %fileList;
}
Aside from that petty thing, I think the logic is sound. (=
Halide
09-18-2002, 11:22 AM
Wow, thanks for your input guys!
read this rambling quickly, for my newbie sake:
Being the intuitive person i am (hehe...) i figured out that i could alternatively store this "array" in an "object" not even requiring a returned item...now again, I question this. I always thought functions should return something instead of setting a "global" which you then reference somewhere else. This kind of makes the program itself harder to follow I guess.
On a side note: I love tribes 2 scripting and I think it must be the best game script engine ever, if not one of the best scripting languages.
And yes, it supports "do", "while", "switch", and all kinds of goodies (even objects :p even tho I don't know the real reasons for using 'em). One problem I noticed was the @= operator doesn't work (concatenation). Instead I just use <item1> = <item1> @ <item2>
cya
[edit #1] Oh yeah... I'd like to post the new code if I remember
Halide
09-19-2002, 10:36 AM
shazbot I forgot yesterday...bleh maybe I'll remember today
Halide
09-19-2002, 04:20 PM
here is what i did
function HalKillSound::addSoundList(%this, %search, %key)
{
if(isObject(%this.soundList[%key]))
%this.soundList[%key].delete();
%this.soundList[%key] =
new ScriptObject()
{
class = "HalKillSound";
};
for(%fileName = findFirstFile(%search); %fileName !$= ""; %fileName = findNextFile(%search))
%this.soundList[%key].soundFile[%this.soundCount[%key]++] = %fileName;
}
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.