View Full Version : multidy arrays and objects
mediachickie
September 22nd, 2002, 01:16 PM
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!
pom
September 22nd, 2002, 03:29 PM
Can you be more precise about the structure of the code and how you want to output the data?
pom :asian:
mediachickie
September 22nd, 2002, 03:41 PM
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[i][prop1];
l=this[i][prop2];
m+=k+","+l;
}
return m;
}
----
message cont'd to another post..
mediachickie
September 22nd, 2002, 03:46 PM
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!
mediachickie
September 22nd, 2002, 03:49 PM
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[i][prop1];
l=this[i][prop2];
m+=k+","+l;
}
return m;
}
pom
September 22nd, 2002, 03:53 PM
Argh! :P Gimme some time...
And the proto appears incorrectly because of the <. Space your letters:
for (i=0;i < 5;i++)...
pom
September 22nd, 2002, 04:15 PM
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[i].split(","));
}
trace ("aFinal: "+aFinal);
trace ("aFinal.length: "+aFinal.length);
trace ("aFinal[1]: "+aFinal[1]);
trace ("aFinal[1].length: "+aFinal[1].length);A few remarks: 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?pom :asian:
mediachickie
September 22nd, 2002, 06:09 PM
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!!
pom
September 22nd, 2002, 06:18 PM
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:
jsk
September 22nd, 2002, 06:53 PM
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
mediachickie
September 22nd, 2002, 07:04 PM
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!
pom
September 22nd, 2002, 07:04 PM
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:
mediachickie
September 22nd, 2002, 07:13 PM
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!
jsk
September 22nd, 2002, 07:17 PM
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?
mediachickie
September 22nd, 2002, 07:27 PM
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!
mediachickie
September 22nd, 2002, 07:29 PM
and by the way - thanks for the help and the info! this has been great - learning wise!
jsk
September 22nd, 2002, 07:48 PM
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
2. push as many variables as you like into an array
nameDateHolder = new Array()
nameDateHolder.push(combinedNamedate) as often as you need to
3. 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_22 092002
4. Call the lot back from the txt file using loadVariablesNum() with php or whatever
5. Split it back into an array of paired values
myRecoveredArray=myImportedString.split(":")
6. Pull out one of the pairs:
myRecoveredPair=myRecoveredArray.shift()
7. Split this back into its constituents:
myRecoveredPairAfterSplitting=myRecoverdPair.split ("_")
no worries?
mediachickie
September 22nd, 2002, 08:09 PM
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!!
mediachickie
September 22nd, 2002, 08:11 PM
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!
jsk
September 22nd, 2002, 08:20 PM
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.
pom
September 23rd, 2002, 03:42 AM
*bowing down to jsk*
Thanks man. You totally rock.
pom :)
Can anyone rate this thread? I'd like to put it in the Best of Kirupa when we're done with this...
lostinbeta
September 23rd, 2002, 04:03 AM
I rated it a 5. Definitely educational!
mediachickie
September 23rd, 2002, 08:26 PM
this is definitely one of the better threads - learned tons... and tons - thanks everyone!
jsk
September 23rd, 2002, 09:10 PM
thanks, you're very kind.
mediachickie
September 23rd, 2002, 09:56 PM
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.
flex
September 23rd, 2002, 10:07 PM
Hmmm judging by that language you must be in the UK.
mediachickie
September 23rd, 2002, 10:15 PM
actually no -
i'm from canada.
flex
September 23rd, 2002, 10:20 PM
Really? A universal expression then.
sbeener
September 23rd, 2002, 11:50 PM
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[i]){
saveString += j + ":" + objArray[i][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[i].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 (http://chattyfig.figleaf.com/flashcoders-wiki/index.php?ASSetPropFlags)
pom
September 24th, 2002, 04:16 AM
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...
mediachickie
September 24th, 2002, 09:02 AM
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....
sbeener
September 24th, 2002, 01:54 PM
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")];
jsk
September 25th, 2002, 09:44 AM
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.
mediachickie
September 25th, 2002, 11:50 AM
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... :)
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 -- :)
i hope we can continue starting and creating threads like this one..
sheSays
October 18th, 2002, 03:38 AM
hi...anyone has an example of the *.fla file? thanx. :)
EEE
June 30th, 2004, 06:45 AM
anyone who know whether this can done or not ?
a check solution(button) that let user click ,then the answer will come out for the first line ,click second time for the check button, the second line for the answer will be come out.
the answer will be put into a array ,so how i m going to trace the answer out by clicking the button. the array is declared at the action script fo the button.only i button will be use to click all the answer come out step by step .
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.