how to split an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chrisgeary@gmail.com

    how to split an array?

    i have a function (below) which reads the last n lines from a text
    file. rather than read the whole line and output it as is, i want to be
    able to read the line and split the tab delimited text file so I can
    present it in columns, exclude unwanted data etc. can anyone help me
    rewrite the javascript function to achieve this?

    thx

    Chris

    function GetLastLines(fi lespec, lines)
    {
    var fso = new ActiveXObject(" Scripting.FileS ystemObject");
    var f = fso.OpenTextFil e(filespec, 1, false);
    var saLines = new Array();
    while (!f.AtEndOfStre am)
    saLines[saLines.length] = f.ReadLine();
    f.Close( );

    var s = "";

    for (var i = saLines.length - lines; i < saLines.length; i++)
    s += saLines[i] + "<br />";

    return s;
    }


    *** SNIP ***

    <table border=0 cellpadding=0 cellspacing=0>
    <tr><td><font size=1 face=arial>
    <%= GetLastLines("\ \\\server\\shar e\\file.ext", 4) %></font></td></tr>


    </table>

  • Baconbutty

    #2
    Re: how to split an array?

    Have a look at the Array.split(arg ) method.

    "arg" specifies the character or characters where the split will occur.

    "arg" can either be-

    a string, e.g. split("\t")

    a RegularExpressi on, e.g. split(/\t+/)

    Comment

    • chrisgeary@gmail.com

      #3
      Re: how to split an array?

      i've read it several times, but i'm not a programmer so i don't really
      understand it. can you help me rewrite the code so it works?

      thanks

      Chris

      Comment

      • Baconbutty

        #4
        Re: how to split an array?

        I have limited time, so the most I can offer is the following, but it
        is rough. Ultimately if you are not a programmer, how will you be able
        to understand/maintain any suggestion you are given?

        Quick option 1

        Put the lines in a <PRE> element to preserve the existing tabs.

        Option 2 - table format

        What table format do you really want?

        Try the following, untested, rough, not guaranteed in any way:-

        function GetLastLines(fi lespec, lines)
        {
        var fso = new ActiveXObject(" Scripting.FileS ­ystemObject");
        var f = fso.OpenTextFil e(filespec, 1, false);
        var saLines = new Array();
        while (!f.AtEndOfStre am)
        saLines[saLines.length] = f.ReadLine();
        f.Close( );

        var s = "";
        var c;
        for (var i = saLines.length - lines; i < saLines.length; i++)
        {
        s+="<tr>";
        c=saLines[i].split(/\t+/);
        for (var j=0; j<c.length; j++)
        {
        s+="<td><font size=1 face=arial>";
        s+=c[j];
        s+="</font></td>";
        }
        s+="</tr>";
        }
        return s;
        }

        *** SNIP ***
        <table border=0 cellpadding=0 cellspacing=0>< tbody>

        <%= GetLastLines("\ \\\server\\shar ­e\\file.ext", 4) %>
        </tbody></table>

        Comment

        • chrisgeary@gmail.com

          #5
          Re: how to split an array?

          you're right.. i cannot understand or maintain it myself - perhaps it
          is a pointless exercise. thanks for taking the time to reply and to
          help.. it is very much appreciated.

          Comment

          • Baconbutty

            #6
            Re: how to split an array?

            Glad to offer help. Didn't mean to be too abrupt. :-)

            Comment

            Working...