- Home /
javascript: declaring a variable in 'while' loop
i have a script called xmlReader, which does exactly what it says: read strings from an xml-file. now i want to declare a variable for every string, but i'm stuck at how to do that exactly. the code is the following:
import System.Xml;
import System.IO;
var xmlString : String;
var i : int = 0;
function Start()
{
var asset:TextAsset = Resources.Load("test 1");
if(asset != null)
{
var reader : XmlTextReader = new XmlTextReader(new StringReader(asset.text));
while(reader.Read())
{
if(reader.Name == "item")
{
Debug.Log(reader.Name + " label = " + reader.GetAttribute("label"));
xmlString= reader.GetAttribute("label");
Debug.Log(xmlString);
i++;
}
}
}
}
i declared xmlString as global var in the beginning for testing so i could see if anything gets written inside the loop. i was hoping that i could use 'i' as an addition to every variable name, but i couldn't find any syntax that worked with that. i tried things like
xmlString +i = reader.GetAttribute("label");
but it's only producing an error. so how can i dynamically declare variables in a loop?
Answer by Waz · Jul 12, 2011 at 12:56 PM
You can't. I think what you're looking for is an array:
xmlString[i] = ...
Be sure your array is large enough:
val xmlString = new String[100];
that actually makes sense... i was so sure that it had to work somehow that i didn't even think about an array >.< thanks a lot!
Answer by aldonaletto · Jul 12, 2011 at 01:09 PM
A simple way would be to declare xmlString as an Array:
var xmlString = new Array();
And use it as any array:
xmlString[i] = reader.GetAttribute("label");
The Array class is not the more efficient resource in the world, and should be avoided in Updates and other frequently called functions because it is a variant type: the system has a lot of behind-the-scene work to do (check content type, manage array size, etc.). But in your application it seems to be the best alternative, since you don't know beforehand how many lines will be read.
Array is almost never the best alternative, and it's not in this case either. The best would be List, which is much faster than Array, can be resized, and doesn't have dynamic typing issues.
@aldonaletto List is in the .Net library as well. http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
It's simple dotNet, so yes, it's available to javascript users.
You need to of course import the namespace.
import System.Collections.Generic;
var myTransformList : List.< Transform > = new List.< Transform >;
myTransformList.Add( transform );
myTransformList[ i ] = transform;
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F#Generic_List
Read both pages through carefully, and you'll save yourself a lot of array-unhappiness :)
The only difference between Lists in C# and JS is that you do (for example) List<int>
in C# and List.<int>
in JS.
Specifying a location doesn't add it; the element must already exist. testList[1] refers to the second element of the list, since arrays always start at 0.