Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: importing a .txt file | creating variables

  1. #1
    Join Date
    Mar 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default importing a .txt file | creating variables

    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:

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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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)

    Code:
    (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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much for the response.

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

    Code:
    <body>
    
    <script type="text/javascript">
    document.write(numPhotos);
    </script>
    
    </body>
    Unfortunately, nothing came up. =(

    Am I using the script as it was intended?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

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

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think you may have just missed:

    http://www.dynamicdrive.com/forums/s...46&postcount=4
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    This is the full code from the .txt file:

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

  8. #8
    Join Date
    Mar 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    //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_0 3_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&photoF ile2=pic3.jpg&photoCaptionID2=13031&photoCaption2=576f6c662045656c&photoDepth2=32&photoSpecies2=13031&photoFile3=pic4.jpg&photoCaptionID3=13041&photoCaption3=53 756e666c6f7765722053746172&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

  9. #9
    Join Date
    Mar 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm seeing in test.js:

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

    Code:
    function updateValues(){
     new loadXmlHttp('form_data.txt');
     setTimeout(updateValues, 105000);
    };
    There could also be other problems.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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