Multidy arrays and objects

bowing down to jsk

Thanks man. You totally rock.

pom :slight_smile:

Can anyone rate this thread? Iā€™d like to put it in the Best of Kirupa when weā€™re done with thisā€¦

I rated it a 5. Definitely educational!

this is definitely one of the better threads - learned tonsā€¦ and tons - thanks everyone!

thanks, youā€™re very kind.

ohhhhh here we go again folksā€¦stuck on one part and only one part - jks - if you have insight let me know!!

After shifting and splitting - i need to use those vars to populate a listbox. I got this working. However, I need to do it in a loop and my loop keeps screwing up - so it doesnā€™t populate correctly:

CODE:
//-----------------------
function convertString(){

tempArray=theRecoveredString.split("#");
thisVar=tempArray.length;
for(i=0;i>=thisVar;i++){
  myPair=tempArray.shift();
myPairDone=myPair.split("|");
  	_root.message=tempArray.length;
   k=myPairDone[0];
     m=myPairDone[1];
   	allSongs.addItemAt(i,k,m);
	t_obj=k.concat("|",m);
	playlist=new Array();
	playlist.push(t_obj);
	delete t_obj;

}
}

//----------------------------------
okay I know the problem is that since i am shifting elements out of the array - the size of the array gets smaller - changing the whole tempArray.length bit in the loop -----

// ā€“ i put the vars into an array again at the end - so that I am reading to go when I need to join them againā€¦I also used # and | delimiters -

Anyhow -its being a bit of a wanker tonightā€¦ keeps outputting the first set of vars and that is it.

Hmmm judging by that language you must be in the UK.

actually no -
iā€™m from canada.

Really? A universal expression then.

hey all.

iā€™m a latecomer to this thread but i wrote some code i thought iā€™d post anywayā€¦

flex - canada loves: black adder, monty python, absolutely fabulous, red dwarf, etc. thus ā€œwankerā€ is well entwined in our lexicon. so funnyā€¦ : ) canā€™t speak for other nationalities.

mediachick - where in canada?

i love putting things in arrays too. these functions are designed to grab an array of objects containing any number of name / value pairs and turn it into a string where:

  • objects are seperated by ā€œ|ā€
  • name / value pairs are seperated by ā€œ,ā€
  • and name is seperated from value by ā€œ:ā€

and extract it back again, of course.

(ps. put your code in [code] tags for formatting)


function saveDataToString(objArray){
	var i,j,saveString;
	for(i in objArray){
		for(j in objArray*){
			saveString += j + ":" + objArray*[j] + ",";
		}
		saveString = saveString.slice(0,-1);
		saveString += "|";
	}
	saveString = saveString.slice(0,-1);
	return(saveString);
}

function retrieveDataFromString(save){
	var i,j,o,a,b,c,returnedArray = [];
	a = save.split("|");
	for(i in a){
		o = {};
		b = a*.split(",");
		for(j in b){
			c = b[j].split(":");
			o[c[0]] = c[1];
		}
		returnedArray.push(o);
	}
	return(returnedArray);
}

a caveat for using for in loops - if youā€™ve defined custom methods they will appear in your for in loops! ie. if youā€™ve written a cool Array.prototype.spliceIntoArray() method, when you iterate through an array using for in, spliceIntoArray will be iterated over as if it were in the array.

this can break things. the solution is to hide your custom methods using ASSetPropFlags().

for more info, check the wiki

I was wondering, Supra, instead of hiding the method, you could also test the type of the data you get from your for in loop, no? Just wondering.

pom :asian:

And red dwarfā€¦ Come on peopleā€¦

sbeener -
I think you just explained and solved my issues with the ASSetPropFlagsā€“
So if I have any type of extension prototype(Array.prototype.splitIt - lets say or whatever) - its going to show up in any ā€œfor Inā€ loop that I do relating to that object ( array)?

I am going to check the wiki! Iā€™ve read about it but never really got it-

So if made objects much like :
function createNew =function(name,id){
this.name=name;
this.id=id;
}

// and then added those to an array ā€“
myArray=new Array();
temporary_Obj=new createNew(ā€œwankerā€,ā€œ01ā€);
myArray.push(temporary_Obj);
delete temporary_Obj;

//-----------------

This wil be good enough to use the functions you wrote beautifully to convert the object array to a string and then parse it back to an obj array?

Anyone have any pros and cons regarding using obj arrays ? I personally love themā€¦

Iā€™m from the capital - by the way. And yeah, its pretty sad, but english humour really does rule up northā€¦I love absolutely fabulousā€¦

ily - yeah, sure you can test each time. itā€™s just not really as elegant, imho.

mediachick - yup, should do the trick. all the functions need is for the objects in the array to contain only name / value pairs - ie. not other objects or arrays. let me know how it goes.

to shorten your code, you can add an object straight to an array like this:


myArray.push(new createNew("wanker","01"));

the array must exist before hand. you can create and populate an array in one step like this:


myArray = [new createNew("wanker","01")];

Hi Mediachickie - typical ā€¦ I go away for a few days and miss out on all the fun stuffā€¦
Seems like sbeener has you well sorted (if youā€™ll excuse a dreadful pun).
As regards pros and cons of using obj arrays my pingin rua in ActionScripting is that the simplest correct answer (i.e. the one that requires the fewest calculations and thus places the least load on the processor) is always the correct answer - and the easiest one to fix/modify/improve.

jsk - yeah I would have to agree - whatever allows for expansion-and has the least amount of calculations should be the winner. I have learned tons thos about string manipulation through this thread - more than I think that I ever want to learnā€¦ :slight_smile:

if you have a chance , jsk, could you look over the code I posted a couple of postings back in this thread - my loop is all wanky and i would really like to know how to do it ā€“ :slight_smile:

i hope we can continue starting and creating threads like this oneā€¦

hiā€¦anyone has an example of the *.fla file? thanx. :slight_smile: