...

View Full Version : Variable/Array Dump? [SOLVED]



vkidv
03-30-2005, 07:16 PM
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:

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)

vkidv
03-31-2005, 02:06 AM
okay, no response - I'll give another example.

Say all the values in Script 1 have been generated :


// 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:


//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,

Kor
03-31-2005, 08:52 AM
still not very clear...at least not for me....

vkidv
03-31-2005, 10:01 AM
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..

Kor
03-31-2005, 10:11 AM
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>

vkidv
03-31-2005, 10:47 AM
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:


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]

Kor
03-31-2005, 11:04 AM
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?

vkidv
03-31-2005, 11:18 AM
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.

Kor
03-31-2005, 11:30 AM
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

vkidv
03-31-2005, 12:39 PM
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.


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.

Kor
03-31-2005, 12:43 PM
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... :thumbsup:

vkidv
03-31-2005, 12:53 PM
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...

vkidv
04-01-2005, 01:59 AM
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 :

<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..

Harry Armadillo
04-01-2005, 09:31 AM
Recursion is how you deal with multi-dimensional arrays..

Try this
<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>

vkidv
04-01-2005, 10:19 AM
Thanks Harry, This is just what I needed.

Thankyou everyone. :thumbsup:
Just going to change title.

logic
10-19-2005, 10:47 PM
Also check out Javascript Object Notation (http://www.json.org/).



/*
Copyright (c) 2005 JSON.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

Array.prototype.______array = '______array';

var JSON = {
org: 'http://www.JSON.org',
copyright: '(c)2005 JSON.org',
license: 'http://www.crockford.com/JSON/license.html',

stringify: function (arg) {
var c, i, l, s = '', v;

switch (typeof arg) {
case 'object':
if (arg) {
if (arg.______array == '______array') {
for (i = 0; i < arg.length; ++i) {
v = this.stringify(arg[i]);
if (s) {
s += ',';
}
s += v;
}
return '[' + s + ']';
} else if (typeof arg.toString != 'undefined') {
for (i in arg) {
v = arg[i];
if (typeof v != 'undefined' && typeof v != 'function') {
v = this.stringify(v);
if (s) {
s += ',';
}
s += this.stringify(i) + ':' + v;
}
}
return '{' + s + '}';
}
}
return 'null';
case 'number':
return isFinite(arg) ? String(arg) : 'null';
case 'string':
l = arg.length;
s = '"';
for (i = 0; i < l; i += 1) {
c = arg.charAt(i);
if (c >= ' ') {
if (c == '\\' || c == '"') {
s += '\\';
}
s += c;
} else {
switch (c) {
case '\b':
s += '\\b';
break;
case '\f':
s += '\\f';
break;
case '\n':
s += '\\n';
break;
case '\r':
s += '\\r';
break;
case '\t':
s += '\\t';
break;
default:
c = c.charCodeAt();
s += '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}
}
}
return s + '"';
case 'boolean':
return String(arg);
default:
return 'null';
}
},
parse: function (text) {
var at = 0;
var ch = ' ';

function error(m) {
throw {
name: 'JSONError',
message: m,
at: at - 1,
text: text
};
}

function next() {
ch = text.charAt(at);
at += 1;
return ch;
}

function white() {
while (ch !== '' && ch <= ' ') {
next();
}
}

function str() {
var i, s = '', t, u;

if (ch == '"') {
outer: while (next()) {
if (ch == '"') {
next();
return s;
} else if (ch == '\\') {
switch (next()) {
case 'b':
s += '\b';
break;
case 'f':
s += '\f';
break;
case 'n':
s += '\n';
break;
case 'r':
s += '\r';
break;
case 't':
s += '\t';
break;
case 'u':
u = 0;
for (i = 0; i < 4; i += 1) {
t = parseInt(next(), 16);
if (!isFinite(t)) {
break outer;
}
u = u * 16 + t;
}
s += String.fromCharCode(u);
break;
default:
s += ch;
}
} else {
s += ch;
}
}
}
error("Bad string");
}

function arr() {
var a = [];

if (ch == '[') {
next();
white();
if (ch == ']') {
next();
return a;
}
while (ch) {
a.push(val());
white();
if (ch == ']') {
next();
return a;
} else if (ch != ',') {
break;
}
next();
white();
}
}
error("Bad array");
}

function obj() {
var k, o = {};

if (ch == '{') {
next();
white();
if (ch == '}') {
next();
return o;
}
while (ch) {
k = str();
white();
if (ch != ':') {
break;
}
next();
o[k] = val();
white();
if (ch == '}') {
next();
return o;
} else if (ch != ',') {
break;
}
next();
white();
}
}
error("Bad object");
}

function num() {
var n = '', v;
if (ch == '-') {
n = '-';
next();
}
while (ch >= '0' && ch <= '9') {
n += ch;
next();
}
if (ch == '.') {
n += '.';
while (next() && ch >= '0' && ch <= '9') {
n += ch;
}
}
if (ch == 'e' || ch == 'E') {
n += 'e';
next();
if (ch == '-' || ch == '+') {
n += ch;
next();
}
while (ch >= '0' && ch <= '9') {
n += ch;
next();
}
}
v = +n;
if (!isFinite(v)) {
error("Bad number");
} else {
return v;
}
}

function word() {
switch (ch) {
case 't':
if (next() == 'r' && next() == 'u' && next() == 'e') {
next();
return true;
}
break;
case 'f':
if (next() == 'a' && next() == 'l' && next() == 's' &&
next() == 'e') {
next();
return false;
}
break;
case 'n':
if (next() == 'u' && next() == 'l' && next() == 'l') {
next();
return null;
}
break;
}
error("Syntax error");
}

function val() {
white();
switch (ch) {
case '{':
return obj();
case '[':
return arr();
case '"':
return str();
case '-':
return num();
default:
return ch >= '0' && ch <= '9' ? num() : word();
}
}

return val();
}
};



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum