By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

how to split an array?

P: n/a
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(filespec, lines)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filespec, 1, false);
var saLines = new Array();
while (!f.AtEndOfStream)
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\\share\\file.ext", 4) %></font></td></tr>
</table>

Aug 10 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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 RegularExpression, e.g. split(/\t+/)

Aug 10 '05 #2

P: n/a
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

Aug 10 '05 #3

P: n/a
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(filespec, lines)
{
var fso = new ActiveXObject("Scripting.FileS*ystemObject");
var f = fso.OpenTextFile(filespec, 1, false);
var saLines = new Array();
while (!f.AtEndOfStream)
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>

Aug 10 '05 #4

P: n/a
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.

Aug 11 '05 #5

P: n/a
Glad to offer help. Didn't mean to be too abrupt. :-)

Aug 11 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.