PDA

View Full Version : eval Array access


mummla
07-01-2005, 01:09 PM
Hi all,

Take an example like this:

var arr:Array = new Array("a","b","c");
var str:String = "arr[0]";
trace(eval(str));
- traces undefined.

Ofcourse - I could

var arr:Array = new Array("a","b","c");
var str:String = "arr";
trace(eval(str)[0]);
But I am now in a coding situation where i need to evalute Array brackets in a String - how could I do that? Anyone?

Thanks,

Nick

Taff
07-01-2005, 01:17 PM
Ive possibly misunderstood your request, but does:

var arr:Array = new Array("a","b","c");
var str = arr[0];
trace(this["str"]);

help? Hope so!

Taff

mummla
07-01-2005, 01:22 PM
Thanks - but Im afraid not - the problem is I have a string with array access brackets I need to evaluate.

nitinmukesh123
07-01-2005, 01:43 PM
Try this

var arr:Array = new Array("a","b","c");
for(var i:Number = 0;i<arr.length;i++) {
var str:String = "arr["+i+"]";
trace(_root[str.substring(0,str.indexOf("["))][str.substring(str.indexOf("[")+1,str.length-1)]);
}

Billystyx
07-01-2005, 02:00 PM
var arr:Array = new Array("a","b","c");
str=new String(arr[0]);
trace(str);

billystyx

mummla
07-01-2005, 02:10 PM
nitinmukesh123

- Many thanks. Yes Im starting to believe the only way to do it is to start chopping strings, just hoping there was an easier way to do it - with a String like "DATA.SECTION[2].AREA[3].TEXT[55]" Im afraid that extensive processing would cause performance issues, which is exactly what I´m trying to avoid in this case - translating xpath strings to object references.

Billystyx
07-01-2005, 02:29 PM
what is the full picture of what you are trying to achieve?
and what should
DATA.SECTION[2].AREA[3].TEXT[55]
be looking for?

if section, area and text are all arrays, then it should look like:

DATA[section[2]][area[3]][text[55]]

to give you the array values of each of thsoe. But maybe I misunderstand.

mummla
07-01-2005, 02:39 PM
Billystyx-

Thanks for your answers - but the issue is that "DATA.SECTION[2].AREA[3].TEXT[55]" would be a String - not a direct reference. So, to access whatever is in "TEXT[55]" I need to evaluate the String - which is the problem, not the actual access.

The full picture of what I am trying to achieve is to convert an Xpath querystring, for example "//DATA/SECTION[2]/AREA[3]/TEXT[55]" to an object reference - DATA.SECTION[2].AREA[3].TEXT[55]

Xeef
07-01-2005, 03:30 PM
DATA = {};
DATA.SECTION = [];
DATA.SECTION[2] = {};
DATA.SECTION[2].AREA = [];
DATA.SECTION[2].AREA[3] = {};
DATA.SECTION[2].AREA[3].TEXT = [];
DATA.SECTION[2].AREA[3].TEXT[55] = "Hallo";
X = "//DATA/SECTION[2]/AREA[3]/TEXT[55]";
X = X.substr(2).split("/");
Q = _root[X.shift()];
X = X.join("[").split("[");
X = X.join("]").split("]");
do {
A = X.shift();
B = X.shift();
X.shift();
Q = Q[A][B];
} while (X.length);
trace(Q);

mummla
07-01-2005, 04:16 PM
Thanks reef - nice solution! Tried to modify it so it would be general to situations where not every subobject is an Array, for example "//DATA/TEXTS/TEXT[3]/HEADER", but I'm kind of stuck..

Xeef
07-01-2005, 04:39 PM
DATA = {};
DATA.SECTION = [];
DATA.SECTION[2] = {};
DATA.SECTION[2].AREA = [];
DATA.SECTION[2].AREA[3] = {};
DATA.SECTION[2].AREA[3].TEXT = [];
DATA.SECTION[2].AREA[3].TEXT[55] = "Hallo";
//
DATA.TEXTS = {};
DATA.TEXTS.TEXT = [];
DATA.TEXTS.TEXT[3] = {};
DATA.TEXTS.TEXT[3].HEADER = "Xeef";
//trace(DATA.TEXTS.TEXT[3].HEADER)
//
X = "//DATA/SECTION[2]/AREA[3]/TEXT[55]";
QQQ(X);
X = "//DATA/TEXTS/TEXT[3]/HEADER";
QQQ(X);
//
//
function QQQ(X) {
X = X.substr(2).split("/");
Q = _root[X.shift()];
X = X.join("[").split("[");
X = X.join("]").split("]");
trace(X);
do {
A = X.shift();
if (X[0] == Number(X[0])) {
B = X.shift();
X.shift();
Q = Q[A][B];
} else {Q = Q[A]
}
} while (X.length);
trace("---> "+Q);
}

mummla
07-01-2005, 05:17 PM
Xeef! Thank you so much - you are the man.