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


Groups > comp.lang.javascript > #32583

Re: File reading

Newsgroups comp.lang.javascript
Date 2017-02-09 23:16 -0800
References <444d98f0-baf6-4c2a-a36b-712ea50fc739@googlegroups.com>
Message-ID <301b665d-d492-4ebf-a89e-2ec8b542c828@googlegroups.com> (permalink)
Subject Re: File reading
From jonas.thornvall@gmail.com

Show all headers | View raw


Den fredag 10 februari 2017 kl. 07:18:18 UTC+1 skrev jonas.t...@gmail.com:
> I have saved files with "graphtext" each node is stored on a single line and separated with newline /r/n.
> 
> Every line have several dataitems, each dataitem is separated by "*".
> 
> Each node can have "no links" or a number of links named nodelinks, the nodelinks is separated using ",".
> 
> When i split text into noderows and then split the noderows inot dataitem array, sometimes the node have no links "nodelinks is undefined"= dataitem[1], so it can't be split. 
> 
> I try to look if there is anything to split in dataitem[1] "nodelinks" but of course sometimes it is undefined.
> I forgot totally how to deal with such situations?
> 
> How can i check if dataitem[1] is undefined and how do i deal with it?
> I also wonder if dataitem[1]=="1" and i split will there be one element in nlinks? var nlinks=dataitem[1].split(",");
> 
> 
> 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;
> 
>         var radarray=textFromFileLoaded.split("\r\n");
>     nodes=radarray.length-1;
>     //document.NoNodes.nodeval.value=nodes;
>     for (var m = 0; m <radarray.length ; m ++ )
>     {
>       arr[m] ={nodelinks : [], hold : false, rposX : 0, rposY : 0, rwidth : 10, rheigth : 10, rcolor : "red" , bcolor : "green"};
>     }
>     var x=0;
>     while (x<radarray.length){
>        var dataitem=[];
>        dataitem=radarray[x].split("*");
>       ?????????????????????????????????????????????????????????
>       if (dataitem[1]!="") { var nlinks=dataitem[1].split(",");
>        var b=0;
>           while (b<nlinks.length) {
>              arr[x].nodelinks[b]=parseInt(nlinks[b]);
>              b++;
>           }
>       }
>       ????????????????????????????????????????????????????????? 
>        arr[x].hold=false;
>        arr[x].rposX=parseInt(dataitem[3]);
>        arr[x].rposY=parseInt(dataitem[4]);
>        arr[x].rwidth=parseInt(dataitem[5]);
>        arr[x].rheigth=parseInt(dataitem[6]);
>        arr[x].rcolor=dataitem[7];
>        arr[x].bcolor=dataitem[8];
>        x++;
>     }
>     };
>     fileReader.readAsText(fileToLoad, "UTF-8");
> }

Solved it !== had to check against the datatype.

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


Thread

File reading jonas.thornvall@gmail.com - 2017-02-09 22:18 -0800
  Re: File reading jonas.thornvall@gmail.com - 2017-02-09 23:11 -0800
  Re: File reading jonas.thornvall@gmail.com - 2017-02-09 23:16 -0800
    Re: File reading "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-02-10 09:00 +0100
      Re: File reading jonas.thornvall@gmail.com - 2017-02-10 00:45 -0800
        Re: File reading "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-02-10 09:57 +0100
          Re: File reading jonas.thornvall@gmail.com - 2017-02-10 01:27 -0800

csiph-web