Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32267

Re: Read in file to array.

Newsgroups comp.lang.javascript
Date 2017-01-23 07:41 -0800
References <2b6a77ee-b20d-4f11-bd56-c73f66523ec7@googlegroups.com> <015aa987-b329-446c-9fb9-b7c1317a7a39@googlegroups.com> <30277451-ca92-4bc3-b070-3ed4fd8d2dfc@googlegroups.com>
Message-ID <191ace72-7818-44ca-9dae-d0499239f132@googlegroups.com> (permalink)
Subject Re: Read in file to array.
From jonas.thornvall@gmail.com

Show all headers | View raw


Den måndag 23 januari 2017 kl. 15:27:03 UTC+1 skrev jonas.t...@gmail.com:
> Den måndag 23 januari 2017 kl. 12:59:19 UTC+1 skrev jonas.t...@gmail.com:
> > Den måndag 23 januari 2017 kl. 12:29:37 UTC+1 skrev jonas.t...@gmail.com:
> > > Well i come so far i can save the graph data to a textfile i did go for "\r\n" as node/record separator and i use star "*" to separate data-items in each node/record.
> > > 
> > > So i end up with something like this in my textfile
> > > 0*1,3,7*false*847*122*10*10*#F6A3D3*#F6A3D3
> > > 1*0,5,6*false*950*370*10*10*#D3D85E*#D3D85E
> > > 2*4,6,7*false*352*617*10*10*#7C9F6D*#7C9F6D
> > > 3*0,5,7*false*600*20*10*10*#A5DD6F*#A5DD6F
> > > 4*2,5,6*false*250*369*10*10*#994CF6*#994CF6
> > > 5*1,3,4*false*352*122*10*10*#76A9EE*#76A9EE
> > > 6*1,2,4*false*599*720*10*10*#B4E9C8*#B4E9C8
> > > 7*0,2,3*false*832*549*10*10*#994CF6*#994CF6 
> > > 
> > > Now i want to read back a graph to array in script.
> > >  arr[m] ={nodelinks : [], hold : false, rposX : 0, rposY : 0, rwidth : 10, rheigth : 10, rcolor : "red" , bcolor : "green"};
> > > 
> > > I was thinking sub=arr.split("\r\n")
> > > And then for each record dataitem=sub.split("\r\n")
> > > 
> > > And last for links=dataitem[1].split(",")
> > > 
> > > Does it seem sound?
> > 
> > function loadGraph()
> > {
> >     
> >     var fileToLoad = document.getElementById("fileToLoad").files[0];
> >     var fileReader = new FileReader();
> >     fileReader.onload = function(fileLoadedEvent) 
> >     {
> >         var textFromFileLoaded = fileLoadedEvent.target.result;
> >         document.debug.data.value += textFromFileLoaded;
> >     };
> >     fileReader.readAsText(fileToLoad, "UTF-8");
> > 
> >     arr=[];  
> >     sub=textFromFileLoaded.split("\r\n");
> >     for (var x=0;x<sub.length;sub++){
> >        dataitem=sub[x].split("*");
> >        for(var a=0;a<dataitem.length;a++){
> >           arr[a].hold=dataitem[3];
> >           arr[a].rposX=dataitem[4];
> >           arr[a].rposY=dataitem[5];
> >           arr[a].rwidth=dataitem[6];
> >           arr[a].rcolor=dataitem[7];
> >           arr[a].bcolor=dataitem[8];
> >           dataitem[2].split(",")
> >           for (var b=0;b<dataitem;b++) {
> >              arr[a].nodelinks[b]=dataitem[b]
> >           }
> >        } 
> >     }           
> > }
> 
> Well now the gist seem to work but there must be some problem with javascript
> I manage to get an alert on the data if i "load twice", isn't that strange.
>   
> alert(textFromFileLoaded);
> sub=textFromFileLoaded.split("!");
> 
> "X3LOADSAVE.js:46 Uncaught ReferenceError: textFromFileLoaded is not defined
>     at loadGraph (X3LOADSAVE.js:46)
>     at HTMLButtonElement.onclick (0GRAPH.HTML?filename=N8L3circle.txt:31)"
> 
> But second time file loaded it is read into array without errors reported, it seem like a timing issue?
> 
> There is no problem to just write to the textarea first time but for some reason i can not use textFromFileload at all i must copy the value to my own variable and even then it will not be read in the first time i load, but the second time i press button to load it work.
> 
> function loadGraph()
> {   
>     textFromFileLoaded="";
>     var fileToLoad = document.getElementById("fileToLoad").files[0];
>     var fileReader = new FileReader();
>     fileReader.onload = function(fileLoadedEvent) 
>     {
>         textFromFileLoaded = fileLoadedEvent.target.result;
>         document.debug.data.value = textFromFileLoaded;
>         mytext=textFromFileLoaded;
>     };
>     fileReader.readAsText(fileToLoad, "UTF-8");
>     //arr=[];  
>     
>     alert(mytext);
>     sub=mytext.split("!");
>     for (var x=0;x<sub.length;sub++){
>        dataitem=sub[x].split("*");
>        alert(dataitem[1]);
>        for(var a=0;a<dataitem.length;a++){
>           arr[a].hold=false;
>           arr[a].rposX=parseInt(dataitem[4]);
>           arr[a].rposY=parseInt(dataitem[5]);
>           arr[a].rwidth=parseInt(dataitem[6]);
>           arr[a].rheigth=parseInt(dataitem[7]);
>           arr[a].rcolor=dataitem[8];
>           arr[a].bcolor=dataitem[9];
>           links=dataitem[1].split(",");
>           for (var b=0;b<dataitem;b++) {
>              arr[a].nodelinks[b]=parseInt(links[b]);
>           }
>        } 
>     }
> }

A real stupid way would be to have a listener upon the textarea to to call another function that actually write the data to array, because when that is written one know the value can not be undefined? 
And even if it is one could copy the values from the field doing the readin to array in another file.

But i must say it feel like a workaround.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 03:29 -0800
  Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 03:59 -0800
    Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 06:26 -0800
      Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 07:41 -0800
        Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 08:08 -0800
          Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 08:43 -0800
            Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 09:41 -0800
              Re: Read in file to array. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-23 10:29 -0800
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 11:24 -0800
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 11:38 -0800
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 11:56 -0800
                Re: Read in file to array. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-23 20:36 +0000
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 19:54 -0800
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 20:10 -0800
                Re: Read in file to array. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-24 14:27 +0000
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 08:24 -0800
                Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-24 17:42 +0100
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 08:54 -0800
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 09:04 -0800
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 11:07 -0800
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 13:17 -0800
                Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-24 21:35 +0000
                Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-24 22:42 +0100
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 15:32 -0800
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 15:55 -0800
                Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-25 08:59 +0000
                Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-25 01:58 +0100
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 18:51 -0800
                Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-25 10:29 +0100
                Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-25 08:58 +0000
                Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 12:28 -0800
      Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-24 17:21 +0000

csiph-web