|
| 1 - 22 of 22 [Total 1 Pages] |
|
|
Overloading Arrays for multi-dimensional purposes Jul 26, 2002, 04:14 |
1 |
|
VARIABLE1,VARIABLE1,PROPERTY1,PROPERTY2...PROPERTY6|/ You have a number of lines - in the above format. They are in a single variable. You want to take it and create a multi-dimensional Array. MORE than that - you want it more in an hash ( list, dictionary ) format. I thought it couldn't be done in JS. I finally hit a problem which i couldn't overcome without working out how to handle this. For anyone that doesn't know how these look: Code: [VARIABLE 1a] | - [VARIABLE 2a] = [ PROPERTY 1, PROPERTY 2 ...] | - [VARIABLE 3a] = [ PROPERTY 1, PROPERTY 2 ...] | - [VARIABLE 4a] = [ PROPERTY 1, PROPERTY 2 ...] | - [VARIABLE 5a] = [ PROPERTY 1, PROPERTY 2 ...][VARIABLE 1b] | - [VARIABLE 2b] = [ PROPERTY 1, PROPERTY 2 ...] | - [VARIABLE 3b] = [ PROPERTY 1, PROPERTY 2 ...] | - [VARIABLE 4b] = [ PROPERTY 1, PROPERTY 2 ...] | - [VARIABLE 5b] = [ PROPERTY 1, PROPERTY 2 ...] Now you want to be able to access these by name but not have multiples, and not overwrite the previous value. The method I've ended up with looks like this: Code: MAKES = new Array();TYPES = new Array();var str='/ECMN,AVIS,FORD,MONDEO,GREEN,1.5|/ECMN,HERTZ,FORD,MONDEO,GREEN,1.5|/ECAR,HERTZ,FORD,MONDEO,GREEN,1.5|/';var all = str.split(//|/);for (i=0;i<all.length;i++){ var set = all[i].split(/,/); if (!TYPES[set[0]]){ TYPES.push(set[0]); TYPES[set[0]] = 1; } if (!MAKES[set[1]]){ MAKES.push(set[1]); MAKES[set[1]] = 1; } l=set[0]+"['"+set[1]+"']=new Array('"+set[2]+"','"+set[3]+"','"+set[4]+"','"+set[5]+"')"; try { eval(l); } catch(e) { eval(set[0]+' = new Array();'); eval(l); } }alert(eval(TYPES[1]+'["'+MAKES[1]+'"]'));alert(MAKES);Effectively this gives you the ablility to say: alert ( ECMN['AVIS'] ) and you'll get an array returned containing Ford Mondeo Green 1.5! Now i don't know if i'm being stupid - but to me that seems like a very slight breakthrough. Am i rediscovering the wheel, and is there a common way of doing this? Can anyone think of how to improve this? Flawless
|
|
|
Jul 26, 2002, 09:14 |
4 |
|
Oh - you mean - without evaluation the names...
Well:
set[0] in one iteration may = 'ECMN'
if you weren't to evaluate it - it would either take set[0] as the value held in it - or the literal of the name... either way - not what we'd want.
BUT - the only reason i'm doing it that way is because i'm generating the arrays automatically.
If you weren't - then you're fine saying:
array['NAME'] = new Array( ARRAYREF1, ARRAYREF2 ... );
where ARRAYREFS are arrays to their own right. That example takes it to a 4th dimension though - i think.
In this example you'd end up - for sure - with array.length as 0. To iterate you'd have to use:
for (name in array)
and in that case name would recieve the literal value NAME - so you'd have to then use: array[NAME] - to pull up the array referenced - and then indexing ( or a second level of naming ) to pull up the 3rd or 4th dimensions respectively.
Flawless
|
|
|
Jul 26, 2002, 19:24 |
5 |
|
lol! How spooky - earlier today I used the "for x in y" statement to finally create a neater solution to my character entity problem than what I was originally going for!!! :-)
|
|
|
Jul 27, 2002, 03:21 |
7 |
|
oh and flawless, you should post somemore of your stuff that you've done, i'm sure others will think HTFDHDT (How The F@ck Did He Do This). As i'm still trying to work it out on things like the wire meshes for 3d graphics etc.
|
|
|
Jul 27, 2002, 07:16 |
9 |
|
Andrew - it's not a fix, but it is a better overall solution (in the end) than my original approach. I'll "post up" !!!!
|
|
|
Jul 27, 2002, 07:19 |
10 |
|
Marco - can we take a look at a real fix for your problem then... if you haven't yet found one?
Flawless
|
|
|
Jul 26, 2002, 05:21 |
11 |
|
Now something I *hadn't* thought of was setting the length of the array to be the name of the new array name. That way - when you call ECMN.length - instead of returning 0 (although techically it pseudo holds four or so values) - it would return the number of true elements. THAT way - you can then use ECMN to query what arrays it holds as if it really were a multi-dimensional hash/list/dictionary - RATHER than just a multi-dimensional array. The change is with the variable s and the evaluation of it: Code: <script language="Javascript">MAKES = new Array();TYPES = new Array();var str='/ECMN,Avis,Ford Mondeo 1.6,32.5,1|/ECMN,Hertz,Vauxhall Astra 1.5,35.1,1|/ECMN,Option,Vauxwagon Jetta 1.4,29.5,1|/ECMN,Sixt,Lexus IS300,41.2,0|/ECAR,Avis,Mercedez C200,42.1,0|/ECAR,Hertz,BMW 318i,45,1|/ECAR,Option,Vauxwagon Passat 1.5,27.1,1|/ECAR,Budget,Ford Scorpio 1.8,37.2,1|/ECAR,Sixt,Skoda Octavia 1.9,35.4,1|/CDMN,Avis,Ford Focus 1.3,21.5,1|/CDMN,Hertz,Peugeot 207 1.4,25.1,0|/CDMN,Budget,Nissan Micra 1.1,16.1,1|/CDMN,Option,Vauxhall Corsa 1.2,15.3,1|/LDAR,Avis,Mercedez SLK 500,82,1|/LDAR,Hertz,BMW 850csi, 95.2,0|/LDAR,Sixt,Lexus GS 500, 89.2,1|/';var all = str.split(//|/);for (i=0;i<all.length;i++){ var set = all[i].split(/,/); if (!TYPES[set[0]]){ TYPES.push(set[0]); TYPES[set[0]] = 1; } if (!MAKES[set[1]]){ MAKES.push(set[1]); MAKES[set[1]] = 1; } l=set[0]+"['"+set[1]+"']=new Array('"+set[2]+"','"+set[3]+"','"+set[4]+"')"; s=set[0]+"["+set[0]+".length]='"+set[1]+"'"; try { eval(l); } catch(e) { eval(set[0]+' = new Array();'); eval(l); } eval(s); }var COMPANIES = new Array();var COMPANIES = new Array();COMPANIES['AVIS'] = new Array('image/r_avis.gif','#FFFFFF');COMPANIES['HERTZ'] = new Array('image/r_hertz.gif','#EAEAEA');alert(ECMN[ECMN[0]]);You can see that with ECMN[ECMN[0]] we have direct access to the array inside it by calling it's name This allows our pseudo for: for (i=0;i<ECMN.length;i++){ ECMN[ECMN[i]] << this recieves the array object. } NOW - we could either use the default "for variable in object" OR we could create our own constructer with a new function - and call it progressively - though that would take a lot of work. Is anyone interested in this at all? Flawless
|
|
|
Jul 27, 2002, 08:17 |
13 |
|
Fixed - - Sorry about that.
I'm SURE i've heard the second part of that expression before... ...
"Why can't you get it in first time - it's short enough!"
he he
Flawless
|
|
|
Jul 27, 2002, 09:19 |
14 |
|
Indeed! lol (I don't like to brag, but I've never encountered that problem myself! )
|
|
|
Jul 27, 2002, 09:24 |
15 |
|
You see i always thought that the suit must have been making up for something
Flawless
|
|
|
Jul 26, 2002, 07:19 |
16 |
|
That's fantastic, but like you said...not sure if I have a valid application for it other than testing it on my machine and saying, "CL!"
What does catch() do?
|
|
|
Jul 26, 2002, 07:31 |
17 |
|
Well no - i had a need for it - so i created it.
try {} catch (e){} is an error handling mechanism.
Flawless
|
|
|
Jul 26, 2002, 07:38 |
18 |
|
so it works like this?
try { [...snippeta...] } catch(e){ [...snippetb...] }
If 'snippeta' errors then run 'snippetb' and suppress the error.
Do I have that right?
|
|
|
Jul 26, 2002, 07:40 |
19 |
|
yes - and e recieves the error.
There's more complexities to it if you need them... but in this case i didn't.
Flawless
|
|
|
Jul 26, 2002, 08:42 |
20 |
|
Rather than needing to create your own handler for the members - why not just use the for (obj in obj) statement - which isn't based on length!
This means we can run it with: if (!CLASSES[set[0]]) CLASSES.push(set[0]);// CLASSES[set[0]] = 1; } if (!COMPS[set[1]]) COMPS.push(set[1]);// COMPS[set[1]] = 1; }
Rather.
Flawless
|
|
|
Jul 26, 2002, 08:44 |
21 |
|
or rather:
if (!CLASSES[set[0]]) CLASSES[set[0]] = 1; } if (!COMPS[set[1]]) COMPS[set[1]] = 1; }
If you're doing it backwards ( name based )
Flawless
|
|
| 1 - 22 of 22 [Total 1 Pages] |
|
|
|