Multidy arrays and objects

alrighty - somewhat stuck here…

I have an array that is populated with objects. Each object has two properties. I can access the properties using myarray[1].name — that kinda idea.

I have no problem making the objects and array of them - now - for what is giving me a headache…

I want to output this array of objects into some format - whether it be XML - or a simple text file. I am having problems converting the array to a string for the text file -

Anyone have ideas or tutorials - i just need a push in the right direction!!

Thanks!

Can you be more precise about the structure of the code and how you want to output the data?

pom :asian:

sure!

  1. I have an array of objects. (objArray)
  2. each object has two properties - name and date
  3. These objects are created using :

function createObj(name, date){
this.name=name;
this.date=date;
}

Any object created by the above constructor is put into the array like this:
// declare temp var to hold the object
tempvar=new createObj(thename, thedate);
// add the temp var to the array that holds all the objects
objArray.push(tempvar);
//delete the instance of the object as it exists in the array
delete tempvar;


all of that works fine - i have no prob referencing it or what not. Now - what I basically want to do it to be able to take this ObjArray ( object array) and save it in a text file or XML file - with the intention of retrieving it and recreating the structure once again. Does that make sense?

I thought that one way to do it - would be to convert the arrray to string - using two delimiters to signify object and properties- and I came up with this:

Array.prototype.multiString=function(prop1,prop2){
var i, j, k;
trace(“the length”+ “”+this.length);
for(i=0;i<this.length;i++){
m+="|";
k=this*[prop1];
l=this*[prop2];
m+=k+","+l;
}
return m;
}


message cont’d to another post…

cont’d

now the above prototype is likely not the most efficient - but it does output the object and the properties much like
|prop1,prop2|propA,propB|

which is good for storing the info. I guess I got that far. Now
I need to retrieve it - or load it back and parse it back into the array—SO that is what I am working on right now.

if I am doing anything questionable- non-standard- inefficiently- please let me know! I’m kinda new to this part of it - and really want to learn the proper/correct way to approach something like this.

any suggestions/ideas are appreciated and most welcome!

sorry noticed that the prototype showed up incorrectly:

Array.prototype.multiString=function(prop1,prop2){
var i, m, k;
trace(“the length”+ “”+this.length);
for(i=0;i<this.length;i++){
m+="|";
k=this*[prop1];
l=this*[prop2];

	m+=k+","+l;

}
return m;

}

Argh! :stuck_out_tongue: Gimme some time…

And the proto appears incorrectly because of the <. Space your letters:
for (i=0;i < 5;i++)…

Well, I don’t know how efficient this is either, but here it is anyway…

sString="|prop1,prop2|propA,propB|prop3,prop4";
var S=sString.split("|");
S.reverse();
S.pop();
S.reverse();
trace ("S: "+S);
trace ("S.length: "+S.length);
aFinal=new Array();
for (var i=0;i < S.length;i++){
	aFinal.push(S*.split(","));
}
trace ("aFinal: "+aFinal);
trace ("aFinal.length: "+aFinal.length);
trace ("aFinal[1]: "+aFinal[1]);
trace ("aFinal[1].length: "+aFinal[1].length);

A few remarks: [list][]if your really starts with |, you have to do the reverse/pop/reverse thing to get rid of the first empty element[]Chances are this is going to be quite long. I’m sure there has to be a faster way to parse, but also to do the first part. I’ll check. Or if anyone has ideas?[/list]pom :asian:

just so i understand exactly how this is working-

1- split the array according to the delimiter ("|")
2. reverse the array so that the first delimeter is at the end of the array
3. pop it out of the array
4. reverse the array again so that it is back in order
5. split the array by the second delimiter…(",") and populate the new array (final array) with it?

I just want to make sure I get it. ya know…
Gives me a direction to go to…thanks!!

Yep, I assumed that the input you get from the text file (or the XML file or whatever it is would be something like what I called sString.

Then you’re right. I splip, then I’m taking out the first element (pop takes out the last so I had to reverse the array…) and I split each element of the array, which is a string (“prop1,prop2”) for instance.

Is it any good?

pom :asian:

I’m sure there’s a really obvious reason that I’ve failed to spot but why not split the first string (i.e. the one delimited by |) into an array then split the second string (i.e. the one delimited by , into another array -

first = new Array(“a,b,c”, “d,e,f”, “g,h,i”);
second=first[1].split(",");
trace(second[1]);

or if you know that your data always comes in pairs eg
data=new Array (“a1”, “a2”, “b1”, “b2”) then just shift() it out in pairs

okay - i will try both suggestions-
jsk - maybe you didn’t miss anything? i am basically trying to take one long string that hold object props and convert it back into one object array -

i will let ya know how it goes!

Well, that’s what I’ve been trying to do :P, but there was a little problem with the first element.

And shift, I don’t know, I’ve never used it. Can you please explain how it works?

pom :asian:

alrighty-
so ily - the code works fine!
However - i would like to put it back into an object array - rather than a multi array - I am going to work on it and let you know what I come up with!

ilyaslamasse:

shift() removes the first element of an array just as pop() returns the last element. What problem are you having with the first element?

mediachickie:

why do you need the | delimiters at all? Why not take the values our a pair at a time?

ahh okay -
I suppose there is no need for the | delimiter if I can do it in pairs – but this is where I lack experience…

I guess what I want to do it take the pairs out - use the pairs to create an obj which I will push into another array… does that make sense?

If you look back at the beginning of this thread - I make some objects and stored them into an array. Then I took that array and made it a string with the vars so that I can save it to text file or whatever.

Now I need to take those values - and put them back into the object array.

If you have any ideas on how to do this efficiently - let me know! I feel like my code is a bit clumsy… but its getting better!
thanks guys!

and by the way - thanks for the help and the info! this has been great - learning wise!

Stop me if I’m wrong here: you want to record lots of separate name/date pairs, write the lot to an external flat file database, then retrieve the data and split it back into separate name and date variables.

What if you …

  1. declared two variables:

myName=“jsk”
myDate=“22092002” (I know we europeans put the date backwards just to be different)

combinedNamedate=myName.concat("_", myDate);

this will give you something like jsk_22092002

  1. push as many variables as you like into an array

nameDateHolder = new Array()

nameDateHolder.push(combinedNamedate) as often as you need to

  1. Write the lot to an external txt file using php or whatever

nameDateHolder.join(":")

this will convert the array to a string with each element delimited by “:” (or whatever you choose) e.g.

jsk_22092002:mediachickie_22092002:ilyaslamasse_22092002

  1. Call the lot back from the txt file using loadVariablesNum() with php or whatever

  2. Split it back into an array of paired values

myRecoveredArray=myImportedString.split(":")

  1. Pull out one of the pairs:

myRecoveredPair=myRecoveredArray.shift()

  1. Split this back into its constituents:

myRecoveredPairAfterSplitting=myRecoverdPair.split("_")

no worries?

very nice!!! I totally understood everything – I like using objects and then putting them into arrays- but your way is alot EASIER!
I am going to work on it further!!

only thing - after I recoup the var pairs- I want to put them back into an array – recreate exactly how it was at the beginning. I will work on it!

that’s what step 7 does -

myRecoveredPairAfterSplitting is an array therefore

myRecoveredPairAfterSplitting[0] = “jsk”

and

myRecoveredPairAfterSplitting[1] = “22092002”

hope that helps…

By the way if you want to perform arithmetic calculations on the date i.e. if you want 22092002 to be a number and not an eight character string you’ll probably need to force flash to accept if by usning the Number() method.