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!!
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;
}
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];
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:
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.
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 -
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!
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!
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 …
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
push as many variables as you like into an array
nameDateHolder = new Array()
nameDateHolder.push(combinedNamedate) as often as you need to
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.
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!!
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.