50,230 Members
3 added today
250,678 Resources
38 added today

All Devdex   All ASPdex   Current Category
ASPdex > Forums & Newsgroups > Newsgroups > microsoft.public.scripting.jscript Add this category to My Favorites

View Message Thread  (33 replies)

Results 1 - 10 of 34 Next Page »

CSV and regex s.split(",") and empty fields Add this thread to My Favorites
From: Gerry Hickman
Date Posted: 8/28/2009 8:16:00 AM

Hi,

I can use s.split(",") to split a CSV file into fields when the file looks
like this

one, two, three

BUT, in real life, my input file looks like this

one, two,,four,,,seven,eight

Note that some fields are empty. When I try to use s.split(",") it ignores
the empty fields and my array ends up with only five elements instead of
eight. I would like to end up with an eight element array where some fields
contain an empty string.

If it can't be done with s.split(), is there a function that would do this?

Thanks.

--
Gerry Hickman
London (UK)

Re: CSV and regex s.split(",") and empty fields
From: Evertjan.
Date Posted: 8/28/2009 8:49:00 AM

Gerry Hickman wrote on 28 aug 2009 in
microsoft.public.scripting.jscript:

> I can use s.split(",") to split a CSV file into fields when the file
> looks like this
>
> one, two, three
>
> BUT, in real life, my input file looks like this
>
> one, two,,four,,,seven,eight
>
> Note that some fields are empty. When I try to use s.split(",") it
> ignores the empty fields and my array ends up with only five elements
> instead of eight. I would like to end up with an eight element array
> where some fields contain an empty string.
>

Not true!

<script type='text/javascript'>
var t = 'one,two,,four,,,seven,eight';
var n = t.split(',').length;
alert(n);
</script>

returns 8.

using regex:

var n = t.split(/,/).length;

also returns 8.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Re: CSV and regex s.split(",") and empty fields
From: Gerry Hickman
Date Posted: 8/28/2009 2:18:00 PM

Hello,

Sorry, I didn't test properly, and made a mess of the original post. I
should have explained I'm running under WSH, not in a browser. Either
way, when using a string delimiter, it appears to work!

However, when using regex, I don't see the same results you posted
below. Here's the example code - run under cscript

// cscript split.js
// Split string
var t = 'one,two,,four,,,seven,eight';
var a = t.split(",");
var n = a.length;
WScript.Echo(n);     // prints 8
var a = t.split(/,/);
var n = a.length;
WScript.Echo(n);    // prints 5

Do you get the same results?


Evertjan. wrote:
> Gerry Hickman wrote on 28 aug 2009 in
> microsoft.public.scripting.jscript:
>
>> I can use s.split(",") to split a CSV file into fields when the file
>> looks like this
>>
>> one, two, three
>>
>> BUT, in real life, my input file looks like this
>>
>> one, two,,four,,,seven,eight
>>
>> Note that some fields are empty. When I try to use s.split(",") it
>> ignores the empty fields and my array ends up with only five elements
>> instead of eight. I would like to end up with an eight element array
>> where some fields contain an empty string.
>>
>
> Not true!
>
> <script type='text/javascript'>
> var t = 'one,two,,four,,,seven,eight';
> var n = t.split(',').length;
> alert(n);
> </script>
>
> returns 8.
>
> using regex:
>
> var n = t.split(/,/).length;
>
> also returns 8.
>
>


--
Gerry Hickman (London UK)

Re: CSV and regex s.split(",") and empty fields
From: Evertjan.
Date Posted: 8/28/2009 4:46:00 PM

Gerry Hickman wrote on 28 aug 2009 in microsoft.public.scripting.jscript:

[Please do not toppost on usenet]

> Sorry, I didn't test properly, and made a mess of the original post. I
> should have explained I'm running under WSH, not in a browser. Either
> way, when using a string delimiter, it appears to work!
>
> However, when using regex, I don't see the same results you posted
> below. Here's the example code - run under cscript
>
> // cscript split.js
> // Split string
> var t = 'one,two,,four,,,seven,eight';
> var a = t.split(",");
> var n = a.length;
> WScript.Echo(n);      // prints 8
> var a = t.split(/,/);
> var n = a.length;
> WScript.Echo(n);     // prints 5
>
> Do you get the same results?

yes:

var n = t.split(/,/).length;

cscript and IE8 return 5

FF and Chrome return 8


So definitly a serious error in the M$ jscript engine.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Re: CSV and regex s.split(",") and empty fields
From: Gerry Hickman
Date Posted: 8/29/2009 10:55:00 AM

Evertjan. wrote:

> var n = t.split(/,/).length;
>
> cscript and IE8 return 5
>
> FF and Chrome return 8

> So definitly a serious error in the M$ jscript engine.

OK, is there a way to create a new split() function that would accept a
RegEx and work correctly?


--
Gerry Hickman (London UK)

Re: CSV and regex s.split(",") and empty fields
From: Evertjan.
Date Posted: 8/29/2009 12:41:00 PM

Gerry Hickman wrote on 29 aug 2009 in microsoft.public.scripting.jscript:

>
> Evertjan. wrote:
>
>> var n = t.split(/,/).length;
>>
>> cscript and IE8 return 5
>>
>> FF and Chrome return 8
>
>> So definitly a serious error in the M$ jscript engine.
>
> OK, is there a way to create a new split() function that would accept a
> RegEx and work correctly?

What do you think?
and what did you try?
and why do you need the regex version?

I think there is.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Re: CSV and regex s.split(",") and empty fields
From: Gerry Hickman
Date Posted: 8/29/2009 12:51:00 PM

Evertjan. wrote:

>> OK, is there a way to create a new split() function that would accept a
>> RegEx and work correctly?
>
> What do you think?
> and what did you try?
> and why do you need the regex version?
>
> I think there is.

I'd like to design a new one, but need some help to do it.

One other possible workaround would be to hack the regex in order to
"trick" the split method into working correctly. The closest thing I've
found so far is this:

var t = 'one,two,,four,,,seven,eight';
var a = t.split(/(?=,)/);
var n = a.length;
WScript.Echo(n);    // prints 8

but you end up with this (note leading commas)

one
,two
,
,four
,
,
,seven
,eight

I need to get the regex version working, because in real life the string
version will only cope with very simple delimiter arrangements.

Thanks.

--
Gerry Hickman (London UK)

Re: CSV and regex s.split(",") and empty fields
From: Evertjan.
Date Posted: 8/29/2009 1:13:00 PM

Gerry Hickman wrote on 29 aug 2009 in microsoft.public.scripting.jscript:

> I need to get the regex version working, because in real life the string
> version will only cope with very simple delimiter arrangements.
>

var a = t.replace(/,/g,'$comma$').split('$comma$');

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Re: CSV and regex s.split(",") and empty fields
From: Dr J R Stockton
Date Posted: 8/29/2009 4:43:00 PM

In microsoft.public.scripting.jscript message <#ZO38m#JKHA.1488@TK2MSFTNGP03.ph
x.gbl>, Fri, 28 Aug 2009 15:13:36, Gerry Hickman
<gerry666uk2@newsgroup.nospam> posted:
>Hi,
>
>I can use s.split(",") to split a CSV file into fields when the file looks
>like this
>
>one, two, three
>
>BUT, in real life, my input file looks like this
>
>one, two,,four,,,seven,eight
>
>Note that some fields are empty. When I try to use s.split(",") it ignores the
>empty fields and my array ends up with only five elements instead of eight. I
>would like to end up with an eight element array where some fields contain an
>empty string.
>
>If it can't be done with s.split(), is there a function that would do this?


NOW there is.

t = 'one,two,,four,,,seven,eight';

String.prototype.Split = function() {
  var A = [], S= "", J
  for (J=0 ; j<this.length ; J++) {
    if (this.charAt(J)==",") { A.push(S) ; S = "" } else S += this.charAt(J) }
  if (S>"") A.push(S)
  return A }

X = t.Split().join("+")         // one+two++four+++seven+eight

You might check what happens id the first or last character is a comma.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK.   ?@merlyn.demon.co.uk     IE7 FF3 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> tp/bp/delphi/jscr/&c, FAQ items, links.

Re: CSV and regex s.split(",") and empty fields
From: Evertjan.
Date Posted: 8/30/2009 4:31:00 AM

Dr J R Stockton wrote on 29 aug 2009 in
microsoft.public.scripting.jscript:

> NOW there is.
>
> t = 'one,two,,four,,,seven,eight';
>
> String.prototype.Split = function() {
>   var A = [], S= "", J
>   for (J=0 ; j<this.length ; J++) {
>     if (this.charAt(J)==",") { A.push(S) ; S = "" } else S +=
>     this.charAt(J) }
>   if (S>"") A.push(S)
>   return A }
>
> X = t.Split().join("+")         // one+two++four+++seven+eight

This does not help, John,
as the split(',') is working.

It is the split(/,/) that is buggy
in IE and w/cscript.

The OP stipulated that he needed complex regex split in cscript.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Results 1 - 10 of 34 Next Page »

 

Would you like to track this thread?

By adding this News Thread to your Favorites Area you can refer to it later with just a click of the mouse. Also you can optionally be notified instantly whenever there are any replies posted to this Thread. To add this Thread to your Favorites Area just click on the red arrow next to the subject of the thread above Add this thread to My Favorites.



ASP.NET Chart Control.
Charts from your Database.
Live Demo & FREE Trial!

ASP ArticlesThis category has been added to your weekly newsletter
ASP Web Sites
ADSI & WSH BooksThis category has been added to your weekly newsletter
FREE ComponentsThis category has been added to your weekly newsletter
ASP EventsThis category has been added to your weekly newsletter
ASP HeadlinesThis category has been added to your weekly newsletter

CSharp ArticlesThis category has been added to your weekly newsletter
C# Web SitesThis category has been added to your weekly newsletter

SQL ArticlesThis category has been added to your weekly newsletter
SQL Events
SQL HeadlinesThis category has been added to your weekly newsletter
SQL Jobs

Jobs in CaliforniaThis category has been added to your weekly newsletter

XML ArticlesThis category has been added to your weekly newsletter
XML BooksThis category has been added to your weekly newsletter
XML Web Sites
XML Tutorials

free asp host

"Alex Homer"This search has been added to your weekly newsletter

Edit My Favorites Edit Profile & Favorites

 

 

 

 

 




Developersdex Home | ASP | C# | SQL | VB | XML | Gurus
Add Your Link | Add Your Code | FAQ | Advertise | Link To Us | Contact Us |
Copyright © 2010 Developersdex™. All rights reserved.