PDA

View Full Version : importing a .txt file | creating variables



thesprucegoose
03-10-2009, 04:46 PM
All,

I need to import a .txt file into a Javascript file.

The file will contain HEX information that will be generated by a video game.

The HEX has a delimiter (&).

If possible, I'd love to be able to create variables directly from the variables in the HEX:

For example:



photoFile4=pic5.jpg&
photoCaptionID4=13007&
photoCaption4=4769616e742050616369666963204f63746f707573&
photoDepth4=3&
photoSpecies4=13007-13009-13044-13011&


can I take that information from the .txt and create a variable named photoDepth4 and give it a value of 3?

The file will receive new information every 3.5 minutes, but the variable names will always stay the same.

I hope I've explained this correctly. I don't even know where to begin with this and would love any help you could give me.

Thanks so much,

--thesprucegoose

jscheuer1
03-10-2009, 05:47 PM
This method is a little risky because it uses the eval method, so there could be unexpected results. But as long as the the contents of this text file are as you say and don't include any quote characters on either side of the = sign, it should work out. Also, due to the way that eval works, the variables produced will be string values. I see that some/most of them appear already to be strings. This means that if you wish to use them in later code as numbers, something must be done to convert them into valid javascript numbers. I set the update frequency to 105000 milliseconds which is half of the update period for the file, so should be accurate enough. Each time the file is requested it will be with a new query string based on the current time, so any updates to the file will be used (rather than a cached version)


(function(){
function loadXmlHttp(url) {
var f = this;
f.xmlHttp = null;
/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
/*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
try {f.ie = window.ActiveXObject}catch(e){f.ie = false;}
@end @*/
if (window.XMLHttpRequest&&!f.ie||/^http/.test(window.location.href))
f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
else if (/(object)|(function)/.test(typeof createRequest))
f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
else {
f.xmlHttp = null;
// Internet Explorer 5 to 6, includes IE 7+ when local //
/*@cc_on @*/
/*@if(@_jscript_version >= 5)
try{f.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){try{f.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){f.xmlHttp=null;}}
@end @*/
}
if(f.xmlHttp != null){
f.xmlHttp.open("GET",url + '?bust=' + new Date().getTime(),true);
f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
f.xmlHttp.send(null);
}
else alert('Your browser does not support AJAX!'); // substitute your desired request object unsupported code here
}

var re = new RegExp('=([^&]*)&', 'g');

loadXmlHttp.prototype.stateChanged=function () {
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href)))
eval(this.xmlHttp.responseText.replace(re, '="$1";'));
}

function updateValues(){
new loadXmlHttp('txt_2_script.txt');
setTimeout(updateValues, 105000);
};
updateValues();
})();

Change the highlighted txt_2_script.txt to the address of the file.

thesprucegoose
03-10-2009, 06:35 PM
Thank you so much for the response.

I changed the .txt file in the the script and tried the following in my HTML file:



<body>

<script type="text/javascript">
document.write(numPhotos);
</script>

</body>



Unfortunately, nothing came up. =(

Am I using the script as it was intended?

jscheuer1
03-10-2009, 06:38 PM
The variables will be available to other code, it may take a moment, so document.write might not be the best choice. Also there is no:

numPhotos

from what you tell me.

If you want more help:

Please post a link to the page on your site that contains the problematic code so we can check it out.

thesprucegoose
03-10-2009, 06:39 PM
Also a note, this won't be on the web...this will be local only.

After the game is played, all the files (pic1-6.jpg, webcam.jpg, and form_data.txt) are overwritten with the new data, so it's not that it needs to be requested every 3.5 minutes...it's that there will be new files every 3.5 minutes.

jscheuer1
03-10-2009, 06:40 PM
I think you may have just missed:

http://www.dynamicdrive.com/forums/showpost.php?p=188646&postcount=4

thesprucegoose
03-10-2009, 06:43 PM
This is the full code from the .txt file:



product=DeepOcean&build=34&date=2009_03_10&time=10_40_25&location=RLLOYD_laptop&sponsor0=TheRideWorks&rideTime=229&maxDepth=32&photoFolder=RLLOYD_laptop__2009_03_10__10_40_25&numPhotos=6&photoFile0=pic1.jpg&photoCaptionID0=24001&photoCaption0=42617420526179205377696d6d696e67&photoDepth0=22&photoSpecies0=13000-13011&photoFile1=pic2.jpg&photoCaptionID1=24007&photoCaption1=43616c69666f726e696120536561204c696f6e205377696d6d696e67&photoDepth1=22&photoSpecies1=13013&photoFile2=pic3.jpg&photoCaptionID2=13031&photoCaption2=576f6c662045656c&photoDepth2=32&photoSpecies2=13031&photoFile3=pic4.jpg&photoCaptionID3=13041&photoCaption3=53756e666c6f7765722053746172&photoDepth3=9&photoSpecies3=13041&photoFile4=pic5.jpg&photoCaptionID4=13007&photoCaption4=4769616e742050616369666963204f63746f707573&photoDepth4=3&photoSpecies4=13007-13009-13044-13011&photoFile5=pic6.jpg&photoCaptionID5=13009&photoCaption5=43616c69666f726e696120487964726f636f72616c&photoDepth5=8&photoSpecies5=13009-13044-13011&allSpecies=13018-13044-13011-13001-13000-13005-13013-13041-13009-13028-13031-13007-13017


I'd like to get all of those to the left of the = to be variables and all to the right of the = (with the exception of the &) to be it's value.

The name will not change when the file is overwritten, but the value will. I can handle that, I just don't know how to dynamically take the info from the .txt and turn it into variables and values.

thesprucegoose
03-10-2009, 06:44 PM
//Code again...little bit prettier

product=DeepOcean&build=34&date=2009_03_10&time=10_40_25&location=RLLOYD_laptop&sponsor0=TheRideWorks&rideTime=229&maxDepth=32&photoFolder=RLLOYD_laptop__2009_03_10__10_40_25&numPhotos=6&photoFile0=pic1.jpg&photoCaptionID0=24001&photoCaption0=42617420526179205377696d6d696e67&photoDepth0=22&photoSpecies0=13000-13011&photoFile1=pic2.jpg&photoCaptionID1=24007&photoCaption1=43616c69666f726e696120536561204c696f6e205377696d6d696e67&photoDepth1=22&photoSpecies1=13013&photoFile2=pic3.jpg&photoCaptionID2=13031&photoCaption2=576f6c662045656c&photoDepth2=32&photoSpecies2=13031&photoFile3=pic4.jpg&photoCaptionID3=13041&photoCaption3=53756e666c6f7765722053746172&photoDepth3=9&photoSpecies3=13041&photoFile4=pic5.jpg&photoCaptionID4=13007&photoCaption4=4769616e742050616369666963204f63746f707573&photoDepth4=3&photoSpecies4=13007-13009-13044-13011&photoFile5=pic6.jpg&photoCaptionID5=13009&photoCaption5=43616c69666f726e696120487964726f636f72616c&photoDepth5=8&photoSpecies5=13009-13044-13011&allSpecies=13018-13044-13011-13001-13000-13005-13013-13041-13009-13028-13031-13007-13017

thesprucegoose
03-10-2009, 06:57 PM
http://www.wildearthride.com/hexCode/cards_temp.html // HTML File
http://www.wildearthride.com/hexCode/test.js // Your Javascript file
http://www.wildearthride.com/hexCode/form_data.txt // .TXT file.

jscheuer1
03-10-2009, 07:22 PM
I'm seeing in test.js:


function updateValues(){
new loadXmlHttp('\\Cvl-fs02\cvl-data\ETC EnTCo\WildEarth\_WEDOS Deep Ocean project\WildCards\Sample Data\RLLOYD_laptop+2009_03_10+10_40_25\form_data.txt');
setTimeout(updateValues, 105000);
};

I should be seeing:


function updateValues(){
new loadXmlHttp('form_data.txt');
setTimeout(updateValues, 105000);
};

There could also be other problems.

jscheuer1
03-10-2009, 07:29 PM
OK, there are other problems. The word location is used as a variable, but it's reserved. So the page tries to switch to that location. There is no & at the end, so that value gets messed up. Both of these should be able to be dealt with though. But I'm thinking an object will need to be used.

thesprucegoose
03-10-2009, 07:33 PM
That's very odd. I've been uploading the file correctly and making reference to it correctly.

jscheuer1
03-10-2009, 07:59 PM
Well, once you get that worked out, and it really doesn't matter what it shows, as long as there is a valid file that contains the desired data at the location listed in the script.

You can check which file is listed in the script as served by using a browser like Opera or FireFox and examining the content of the test.js script file.

Anyways, because of the use of location, we will need to make an object, here is the new code:


var hextext = {};
(function(){
function loadXmlHttp(url) {
var f = this;
f.xmlHttp = null;
/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
/*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
try {f.ie = window.ActiveXObject}catch(e){f.ie = false;}
@end @*/
if (window.XMLHttpRequest&&!f.ie||/^http/.test(window.location.href))
f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
else if (/(object)|(function)/.test(typeof createRequest))
f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
else {
f.xmlHttp = null;
// Internet Explorer 5 to 6, includes IE 7+ when local //
/*@cc_on @*/
/*@if(@_jscript_version >= 5)
try{f.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){try{f.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){f.xmlHttp=null;}}
@end @*/
}
if(f.xmlHttp != null){
f.xmlHttp.open("GET", url + (/\?/.test(url)? '&' : '?') + 'bust=' + new Date().getTime(), true);
f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
f.xmlHttp.send(null);
}
else alert('Your browser does not support AJAX!'); // substitute your desired request object unsupported code here
}

var re1 = new RegExp('([^=]*)=([^&]*)(&|(\\s*$))', 'g'), re2 = new RegExp(',\\s*$');

loadXmlHttp.prototype.stateChanged=function () {
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href)))
eval('hextext = {' + (this.xmlHttp.responseText.replace(re1, '"$1":"$2",').replace(re2, '')) + '}');
}

function updateValues(){
new loadXmlHttp('txt_2_script.txt');
setTimeout(updateValues, 105000);
};
updateValues();
})();

You may now access the values like so:


hextext.value_name

where value_name is the name of the value you want, ex:


<input type="button" value="test" onclick="alert(hextext.numPhotos);">

Or even:


<div id="numPhotos">
&nbsp;
</div>
<script type="text/javascript">
function numPhotos(){
clearTimeout(numPhotos.timer);
if(hextext.numPhotos)
document.getElementById('numPhotos').firstChild.nodeValue = hextext.numPhotos;
else numPhotos.timer = setTimeout(numPhotos, 300)
}
numPhotos();
setInterval (numPhotos, 105000);
</script>

jscheuer1
03-11-2009, 04:56 AM
Here's a demo of another way to take advantage of the imported data:

http://home.comcast.net/~jscheuer1/side/t_2_s/