Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Split function problem

    Hi all,

    I have a problem with split function in javascript.Here is my coding:

    findMin();

    function findMin(){

    var ForReading = 1, ForWriting = 2; //init fso constant

    var fso = new ActiveXObject("Scripting.FileSystemObject"), f, fc, s;
    var j = fso.OpenTextFile("C:\\Testing3.txt", ForReading);
    var k = fso.OpenTextFile("C:\\Testing23.txt", 2, true, 0);

    while(!j.AtEndOfStream){
    var t = j.ReadLine();
    var myArray=t.split("\n");}
    k.WriteLine(myArray[0]);
    }

    what i want to do is open 2 file,one for read and one for write.I open 1 file that consist of list of filename like:

    morp1.txt
    morp2.txt

    then i will split the file by "\n" and put it on array which in this case is myArray.finally i will output the value of the array on a file.

    the problem is that the output will have one extra line after the actual output for example when i try to write the value of myArray[0] the output will be:

    1.morp1.txt
    2.

    the number just to indicate line number.can anyone tell me how to eliminate the last "invisible" line

    thanks in advance

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Code:
    var myArray=t.split("\n");}
    k.WriteLine(myArray[0]);
    }
    I would try these in descending order of preference:
    Code:
    var myArray=t.split(/\r\n?/);
    
    //or
    
    var myArray=t.replace(/\r/g,"").split("\n");
    
    //or
    
    k.WriteLine(myArray[0].replace(/\r/g,""));

  • #3
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi Arty,

    Thanks for your reply.I tried all three method that you gave me but still doesn't solve the problem.the "invisible" line still appear at the very end of the line.

    I wrote the coding above just to highlight the problem that i have but what i actually want to do is more than just write the value of the array.here is the coding:

    findMin();

    function findMin(){

    var ForReading = 1, ForWriting = 2; //init fso constant

    var fso = new ActiveXObject("Scripting.FileSystemObject"), f, fc, s;
    var j = fso.OpenTextFile("C:\\Testing3.txt", ForReading);
    var k = fso.OpenTextFile("C:\\Testing23.txt", 2, true, 0);

    while(!j.AtEndOfStream){
    var t = j.ReadLine();
    var myArray=t.split("\n");}
    for(u=809; u<=836; u++){
    for(i=0; i<=myArray.length; i++){
    var p = fso.OpenTextFile("D:\\MyTask\\40.09\\Combine\\"+myArray[i]);
    var q = p.ReadAll();
    var myArray2=q.split("\n");
    var r = myArray2[u];
    var minValue = r.substring(59,63);
    k.WriteLine(minValue);
    }
    }
    }

    from the coding you can see that I want to read all line from file testing3.txt than split it into an array because I want the data from the myArray[809] to myArray[836] which explain the for loop.the problem is in this line:

    var p = fso.OpenTextFile("D:\\MyTask\\40.09\\Combine\\"+myArray[i]);

    the error massage is "file not found".this is because the output of myArray[i] is not D:\\MyTask\\40.09\\Combine\\Morp1.txt (which i intend to open) but rather the program try to open filename with the invisible line at the end of Morp1.txt.so it cannot found the file.another method that I tried is using the enumerator as below:

    findMin();

    function findMin(){

    var ForReading = 1, ForWriting = 2; //init fso constant

    var fso = new ActiveXObject("Scripting.FileSystemObject"), f, fc, s;
    file = fso.GetFolder("D:\\MyTask\\40.09\\Combine");
    var j = fso.OpenTextFile("C:\\testing23.txt", ForWriting, true, 0);

    f = fso.GetFolder(file);
    fc = new Enumerator(f.files);
    for(var u=809; u<=836; u++){
    for (fc.moveFirst; !fc.atEnd(); fc.moveNext()){
    s = fc.item();
    var t = fso.OpenTextFile("D:\\MyTask\\40.09\\Combine\\"+s.name);
    var s = t.ReadAll();
    var myArray=s.split("\n");
    var q = myArray[u];
    var minValue = q.substring(59,63);
    j.WriteLine(minValue);
    }
    }
    }

    the above coding have a similar purpose.but the coding doesn't loop to second line(810).it only go to the first line(809) but stop there.

    anyone can advice please help.

    thanks in advance


  •  

    Posting Permissions

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