dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: extracting var not value from .txt

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Posts
    996

    extracting var not value from .txt

    how can I just extract the variable names not the value of the variables.
    Code:
    loadArr("arr.txt");
    function LoadArr(file) {
    	arr_obj = new LoadVars();
    	arr_obj.load(file);
    	arr_obj.onLoad = function(success) {
    		if (success) {
    			variables = arr_obj;
    			trace("variables :"+variables);
    
    		} else {
    			trace("not loaded");
    		}
    	};
    }
    The txt file has
    Code:
    allDresses=dominique,larissa,francesca,octavia,simone,adriana,adriana2
    
    &larissa_ds=pic0,pic1,pic2,pic3,pic4,pic5
    
    &dominique_ds=pic0,pic1,pic2

    The output windon gives me this but I just want to extract the variable names
    Code:
    variables :dominique%5Fds=pic0%2Cpic1%2Cpic2&larissa%5Fds=pic0%2Cpic1%2Cpic2%2Cpic3%2Cpic4%2Cpic5%0D%0A%0D%0A&allDresses=dominique%2Clarissa%2Cfrancesca%2Coctavia%2Csimone%2Cadriana%2Cadriana2%0D%0A%0D%0A&onLoad=%5Btype%20Function%5D
    Thanks in advance

  2. #2
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    Well, let's try this..but I'm not sure...
    In fact, what happens when you load the variables...
    You create a series of objects, or properties within the LoadVars object...
    Now, you could parse the return string...
    This means that you have to manually extract the variables from the string that is returned from the LoadVars object...
    I'll code it for ya now.
    Altruism does not exist. Sustainability must be made profitable.

  3. #3
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Keyone...

    As long as you doing that, why not do it for this chap...

    http://www.flashkit.com/board/showth...hreadid=431021

  4. #4
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    Ok, the quickest trik I got to is the following, it's simple and it works, but there is only one RULE or LIMITATION: you must use a character to IDENTIFY VARIABLE NAMES, a character that you will use ONLY FOR THAT PURPOSE and nowhere else in the text file.
    In my example I used the following char: "_".
    Here is the Actionscript (the only things you must adjust within it is the "ID" value, which must be the same as the character you choose, and the URL to the text file...):
    PHP Code:
    ID escape("_");
    show_ID true;
    myBuffer = new LoadVars();
    myBuffer.onLoad = function(success) {
        if (
    success) {
            
    outputArray = new Array();
            var 
    fullVariableString myBuffer.toString();
            var 
    fullVariableArray fullVariableString.split(ID);
            for (var 
    i=0i<fullVariableArray.lengthi++) {
                var 
    tempItem fullVariableArray[i];
                var 
    tempItemArray tempItem.split("=");
                var 
    tempItemValue tempItemArray[1];
                
    tempItemValue tempItemValue.split("&");
                
    tempItemArray[1] = unescape(tempItemValue[0]);
                if (
    tempItemArray[0].length) {
                    
    outputArray.push({labeltempItemArray[0], valuetempItemArray[1]});
                }
            }
            
    visualizeVariables(show_ID);
        }
    };
    myBuffer.load("index.txt");
    function 
    visualizeVariables(add_ID) {
        
    output "Listing variables loaded into \"myBuffer\" from \"index.txt\"" newline;
        
    output += "BEGIN OF LIST ############################" newline;
        for (var 
    i=0i<outputArray.lengthi++) {
            
    output += "Variable no. " + (i+1) + ": ";
            if (
    add_ID) {
                
    output += "\"" unescape(ID);
            } else {
                
    output += "\"";
            }
            
    output += outputArray[i]["label"] + "\", with value: \"" outputArray[i]["value"] + "\"" newline;
        }
        
    output += "END OF LIST ##############################" newline;
        
    output += "Total variables = " ";";
        
    trace(output);

    for the text file there is no explanation (you should already know how to encode it...
    Any way this is the text I used for my example:
    PHP Code:
    _myName=Ascanio&_mySurname=Colonna&_myAge=22&_myCar=Polo
    Hope this helps!
    Altruism does not exist. Sustainability must be made profitable.

  5. #5
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    Wow sh*it I just got in and saw this. I need to go to bed and wake up with a clear head, to understand this.
    I really thought this was a simple issue of extracting the variable name. This is pretty extreme.
    But thanks so much for this. You really put some serious effort in to this and I am grateful but I really never thought it would be this hard to get 4 variables out of a load object.


    I will spend some time to break it down and try and figure it out.

  6. #6
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    Don't worry bro, the .onLoad function is the only interesting part of it.

    Actually I though about making something like this a long time ago, so that I could use textfiles 100% dynamically to generate the database for my PictureViewer..

    I really hope this can solve your problem!

    G night.
    Altruism does not exist. Sustainability must be made profitable.

  7. #7
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    Hi keyone.it I have been going through your code and there is one thing I can't seem to understand why it was used and that is the escape("_") and then the unecape();
    Also I tried to
    Code:
    trace(outputArray.push({label:tempItemArray[0], value:tempItemArray[1]}));
    just to see how it is using this set up or just to get a visual of what this was doing it gave me.
    Code:
    2
    4
    6
    if I trace(outputArray);
    I get this
    Code:
    [object Object]
    [object Object],[object Object]
    [object Object],[object Object],[object Object]
    can you explain this and what object is going into which.

    One other thing I had to declare an outputArray(); as a new Array()
    before it would work.
    Thanks again

  8. #8
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    anyone
    I can't really doing anything more with this info unless I can understand it.
    Thanks

  9. #9
    Developer
    Join Date
    Sep 2001
    Location
    The Bluegrass State Will Flash For Food ™
    Posts
    3,789
    Just for the fun of it:
    Code:
    LoadVars.prototype.makeVars = function() {
    	this._variables = new Array();
    	this._values = new Array();
    	var _pairs = this.toString().split("&");
    	for (something in _pairs) {
    		thisSomething = _pairs[something].split("=")[0];
    		thisValue = _pairs[something].split("=")[1];
    		if (thisSomething != "onLoad" && thisSomething != "makeVars" &&
    			thisSomething != "%5Fvariables" && thisSomething != "%5Fvalues") {
    			this._variables.push(thisSomething);
    			this._values.push(thisValue);
    		}
    	}
    	return this._variables;
    };

  10. #10
    Developer
    Join Date
    Sep 2001
    Location
    The Bluegrass State Will Flash For Food ™
    Posts
    3,789
    Quick Test:
    Code:
    _loadvars = new LoadVars();
    _loadvars.onLoad = function() {
    	trace(this.makeVars());
    _loadvars.load("test.txt");
    _loadvars._variables list your variables
    _loadvars._values list your values
    Last edited by gSOLO_01; 03-11-2003 at 01:28 PM.

  11. #11
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    The reason why you get the "object object object..." output is because of the actual workings of that kind of array, which is not actually an array, but just a normal object with a series of arrays within it (so the "string" value will be to call the array, and the number value will be to call the item within it - myArray["label"][0]).

    Hope I described the thing in a simple way..

    Cheers!
    Altruism does not exist. Sustainability must be made profitable.

  12. #12
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    Thanks for the explanation. I also wondered why the escape and unescape was used. Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center