Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Variable/Array Dump? [SOLVED]

    Hello,
    Some javascripts of mine generate information which would be useful in other scripts.
    The information are variables arrays and multi-dimensional arrays.

    Are there/is there any software or a javascript that can display/out all active variables/arrays in a format that is ready to be declared, for example:
    Code:
    var vMarray = new Array(
    [1,2,3],
    ["a","b","c"],
    "abc",
    123
    ) ;
    var vVar = "Yes" ;
    As what I need to do is simply grab all these declarations and put it straight into a javascript library file - whatever script that needs the data can then getit (the scripts cannot use the script that actually generates the data)
    Last edited by vkidv; 04-01-2005 at 11:19 AM.
    Regards
    VKiDV

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    okay, no response - I'll give another example.

    Say all the values in Script 1 have been generated :
    Code:
    // Values that Script 1 Generated
    var vMarray = new Array(
    [1,2,3],
    ["a","b","c"],
    "abc",
    123
    ) ;
    var vVar = "Yes" ;
    var vAarray["Nope"] = new Array("No") ;
    The above is what is needed ; it can easily be put into Script 2 for use...

    The code example above is actually not available - it has all been generated/used by in Script 1, for example:

    Code:
    //Script 1 extracts
    vMarray[0][1] = someFunction(2) ;
    // and
    vMarray[2] = someFunction("abc") ;
    // and
    var vAarray[ someFunction("Nope") ] = new Array(someFunction("No") ;
    Script 2 needs all the variables, arrays/associative arrays generated/used by Script 1 in declarable form (that I can put straight into Script 2 to be used)

    I'm sure there is some software or a script to cycle should all variables, arrays/associative arrays and output them (in a usable format)
    Hope someone understands,
    Regards
    VKiDV

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    still not very clear...at least not for me....
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, let me try to explain better...

    I want to dump all variables and arrays - that the script uses/had in memory/had declared - into code.

    I want to put all the variables/arrays from one script to another -- without linking them in any way..
    Regards
    VKiDV

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    the way they are declared in your example, they all are globals. The global variables may be used as they are, nomatter if they are in different script embeded or external files. So that you don't need to "transfere" them from a script to another or to "store" them in a third place... The only condition (if you have external files) is that both files should have loaded in the same page onload.

    <head>
    <script type="text/JavaScript" src="script1.js"></script>
    <script type="text/JavaScript" src="script2.js"></script>
    </head>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is what I can't do -- one script is purely to generate some data which needs to be used on another.

    Its a one of thing, it won't need to be recalculated every time.

    I guess I have to do it manually:
    Code:
    document.write( "var vMarray = new Array(
    [" + vMarray[0][0] + "," + vMarray[0][1] + "," + vMarray[0][2] + "];" ) ;
    I was convinced there would be a better way other than typing out each array manually...
    [typo]
    Last edited by vkidv; 03-31-2005 at 01:58 PM.
    Regards
    VKiDV

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    one script is purely to generate some data which needs to be used on another.
    Do you mean one script is generated by a server application from a data base? Or what?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, just a normal javascript.
    The javascript that does the generating (Script 1) firstly opens a large (static) text file (600kB - on my computer, too big for a javascript), secondly it cycles through each line and extracts the useful information - and last it sets the useful to variables and arrays.

    Now, I want this information elseware - the people who will eventually use this will not have access to the script that does the generating OR the big text file...

    Its a one-off thing - its unlikely I'll change the text file..

    The problem is simply just that, I either spend a millena writing it manually or find some software or a script that displays or cycles through all arrays/variables etc,

    Thankyou.
    Last edited by vkidv; 03-31-2005 at 12:23 PM.
    Regards
    VKiDV

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    well, javascript is a client side language, thus it can not write other files. What you need is probably a server-side application which migh read/write from/to different files on server
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    well, javascript is a client side language, thus it can not write other files. What you need is probably a server-side application which migh read/write from/to different files on server
    Okay I'll make an example - I'm sorry I couldn't be clear before..
    Javascript CAN write to files, client side... it just needs permission to, like a hta file.

    Code:
    Script 1 - (generator.hta)
    Using hta and javascript file to parse text files. Opens the large text file and reads line one by one.
    Extracts useful data and puts into an array.
    
    Script 2 - (client.js)
    Ran by a client - Needs these arrays.
    
    Problem: How do I obtain all the array declarations to put into Script 2
    
    ~~~
    
    Delete client.js
    Read large_text_file.txt
    Run generator.hta and then client.htm
    Remember to decompress first
    
    It would help understanding by reading code
    I didn't think dumping arrays would be so difficult.
    Attached Files Attached Files
    Regards
    VKiDV

  • #11
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    well, as you can see, javascript and hta are different things. You may use as well activeX or XMLHTTP objects in combination with javascript, but not javascript alone... OK, I understand now that you need to write something...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #12
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Arrghhh. I think I confused everybody.

    The writing of files and files have nothing to do with my problem. Neither does client side or server side.. It was supposed to support/explain the problem.. Sorry

    My problem is I simply need all the active arrays in a page. Kor, you suggested external javascript libraries, I cannot put the script inside (generator.hta) that parses the text file in an external JS file as it would not have the permissions OR the text file to work with..

    I messed around with this: http://members.aol.com/paypalhelper/dumpj.htm
    Dumping all objects... The only problem, it doesn't dump arrays in the correct way

    To summarise: I need a list of arrays that was declared in a script. Like all arrays that were made at runtime. I don't know how to do this - so thats why I posted this topic asking if there was any code or even software to show this...

    Kor and whoever confused - please see the attached archive, i hope it can make some things clearer...
    Last edited by vkidv; 03-31-2005 at 05:15 PM.
    Regards
    VKiDV

  • #13
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, I attempted to write an array dump function myself I haven't been able to make it obtain/dump MULTI DIMENSIONAL arrays (which is what I need it to)

    paste this and save as htm/html :
    Code:
    <script>
    /***********************************************************/
    function isAlien(a) {
       return isObject(a) && typeof a.constructor != 'function';
    }
    
    function isArray(a) {
        return isObject(a) && a.constructor == Array;
    }
    
    function isBoolean(a) {
        return typeof a == 'boolean';
    }
    
    function isEmpty(o) {
        var i, v;
        if (isObject(o)) {
            for (i in o) {
                v = o[i];
                if (isUndefined(v) && isFunction(v)) {
                    return false;
                }
            }
        }
        return true;
    }
    
    function isFunction(a) {
        return typeof a == 'function';
    }
    
    function isNull(a) {
        return typeof a == 'object' && !a;
    }
    
    function isNumber(a) {
        return typeof a == 'number' && isFinite(a);
    }
    
    function isObject(a) {
        return (a && typeof a == 'object') || isFunction(a);
    }
    
    function isString(a) {
        return typeof a == 'string';
    }
    
    function isUndefined(a) {
        return typeof a == 'undefined';
    } 
    
    /********
    * code above by Douglas Crockford
    * ( http://www.crockford.com/javascript/remedial.html )
    *
    * code below by vkidv
    *********/
    
    var vMarray = new Array(
    [1,2,3],
    ["a","b","c"],
    "abc",
    123
    ) ;
    // here is an array containing numbers and strings and multi dimensional arrays
    
    function typeCheck( what ) // checks type and returns code that makes it
    	{
    if ( isNumber( what ) ) Code = what ;
    // check if number
    
    else if ( isString( what ) ) Code = "\"" + what + "\"" ;
    // check if string
    
    return Code;
    	}
    
    
    function dumpArray( arrayName, sarrayName ) {
    var Code = sarrayName + " = new Array(" ;
    for ( var cur in arrayName )			// cycle through array
      { Code += "\n"	// new line
    	if ( isArray( arrayName[cur] ) )
    	{ Code += "[" ; for ( var innercur in arrayName[cur] )			// cycle through array inside main array
    		{
    
    	// Here would go some code that cycles through ALL multi dimensional (at any deepness) arrays and then turns it into runnable code with typeCheck() 
    	// I can't think of a way to do this programatically
    
    		Code += "[" + arrayName[cur][innercur] + "]" ; 	// i was trying differing things out here and below
    		  // Code += typeCheck( arrayName[cur][innercur] )
    		} Code += "]" ;
    	}
    	else Code += typeCheck( arrayName[cur] )	// if not a multi dimensional array - then turn straight into code
    	
      }
    return Code + "\n) ;" ;
    }
    
    
    alert( dumpArray( vMarray, "vMarray") ) ;
    </script>
    It is as close as I could get to what was defined (in red)
    Commas are missing..
    Regards
    VKiDV

  • #14
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Recursion is how you deal with multi-dimensional arrays..

    Try this
    Code:
    <html><body><script>
    
    function objectToLiteral(a,ext,namestr){
      var s='{ ',e;
      for(e in a){
        s+=e+':';
        if(a[e]==null)
          s+='null';
        else if(typeof(a[e])=='object'){
          if(a[e].constructor==Array)
            s+=arrayToLiteral(a[e],ext,namestr+"['"+e+"']");
          else if(a[e].constructor==Object)
            s+=objectToLiteral(a[e],ext,namestr+"['"+e+"']");
          }
        else if(typeof(a[e])=='number' || typeof(a[e])=='boolean')
          s+=a[e];
        else if(typeof(a[e])=='string')
          s+="'"+a[e]+"'";
        s+=",";
        }
      return s.substr(0,s.length-1)+" }";
      }
    function arrayToLiteral(a,ext,namestr){
      var s='[ ',e;
      for(var dex=0; dex<a.length; dex++){
        e=a[dex];
        if(e==null)
          s+="null";
        else if(typeof(e)=='object'){
          if(e.constructor == Array)
            s+=arrayToLiteral(e,ext,namestr+'['+dex+']');
          else if(e.constructor==Object)
            s+=objectToLiteral(e,ext,namestr+'['+dex+']');
          }
        else if(typeof(e)=='number' || typeof(e)=='boolean')
          s+=e;
        else if(typeof(e)=='string')
          s+="'"+e+"'";
        s+=",";
        }
      for(dex in a){
        if(parseInt(dex).toString()==dex)continue;
        e=a[dex];
        var s2=namestr+"['"+dex+"']=";
        if(e==null)
          s2+="null";
        else if(typeof(e)=='object'){
          if(e.constructor == Array)
            s2+=arrayToLiteral(e,ext,namestr+"['"+dex+"']");
          else if(e.constructor==Object)
            s2+=objectToLiteral(e,ext,namestr+"['"+dex+"']");
          }
        else if(typeof(e)=='number' || typeof(e)=='boolean')
          s2+=e;
        else if(typeof(e)=='string')
          s2+="'"+e+"'";
        ext.push(s2)
        }
      return s.substr(0,s.length-1)+" ]";
      }
    
    strings=new Array();
    bob=new Array(1,
                  2,
                  [34],
                  'dfg',
                  [5,34,'foo'],
                  false,
                  null,
                  [],
                  {hey:'you',eat:13,whoa:false,dude:{ack:[true,1,'eek']}}
                  );
    bob['meat']=["pork",'beef',4985];
    bob[2]['nero']='fiddle';
    bob[8].dude.ack['bar']='criminy';
    alert('bob='+arrayToLiteral(bob,strings,'bob')+";\n"+strings.join(';\n'));
    
    </script></body></html>

  • #15
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Harry, This is just what I needed.

    Thankyou everyone.
    Just going to change title.
    Regards
    VKiDV


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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