Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > multiline string split problem and fix

Reply
Thread Tools

multiline string split problem and fix

 
 
Brian
Guest
Posts: n/a
 
      12-17-2006
I have been working on a data reception system.. I am still finding my
way around Javascript, though I am accomplishing much.

I just fixed a flaw that was really hard to find. The symptoms are
this:

I get a multiline string returned to Javascript from a Proxy+Google
Maps API GDownloadUrl()
The data, when added to a DOM table looked fine, about 20 lines in CSV
format

Sunrise,-119.098,35.345,0.0<br>
SwanDancer,-119.345,35.567,1.0<br>
.... etc

(I don't know why the <br>'s are there, but that's what it looks like)
So using a suggestion from this newsgroup, I perform two subsequent
split()'s

var index, index2;
var strCSVFile = data;
var arrayCSVFile;

arrayCSVFile = strCSVFile.split( "<br>" );

for ( index = 0; index < arrayCSVFile.length; index++ )
{
arrayCSVFile[ index ] = arrayCSVFile[ index ].split( ',' );
// do stuff to the elements
}

I use both strCSVFile *and* arrayCSVFile to be doubly sure I wasn't
somehow clobbering something, though in theory there needs to be only
the original string. At any rate, what I see is this (after HOURS of
trying and finally using str.charCodeAt())

10|32|32|32|32|32|32|32|32|83|117|110|114|105|115| 101| len=16
10|10|32|32|32|32|32|32|32|32|83|119|97|110|68|97| 110|99|101|114|
len=20
.... etc

%^!@#$^%@ <- that's cursing, people
So I am now hand clipping some number of LF and SPACE chars using
str.charCodeAt(). On top of that, my furtive attempts at RegEx
replacements along the way had been SILENTLY FAILING. Probably because
of the leading LF(s). I had no idea, and it took valuable time..

I looked for split() gotcha's but never found anything like this. I
thought I tried changing the split to "<BR>\r" at one point, but I
probably did the return instead of line feed... Also, that would NOT
handle the first line case ?!

This is what is happening, and I now have tedious code to handle it.
Looking back on the original recv'd data, it does indeed have a leading
LF|SPACE's, with two LF's on every subsequent row. I never saw them.
How could I ? When I aded them to the HTML page to check the data, they
didn't show
This was awful.

FYI

 
Reply With Quote
 
 
 
 
Brian
Guest
Posts: n/a
 
      12-17-2006
btw- I just added

strCSVFile.replace( / /g, '');
strCSVFile.replace( / \n/g, '');
strCSVFile.replace( / \r/g, '');

to clean the data (the whole block before the split()'s. Am I making a
mistake in the RegEx? they don't work...

 
Reply With Quote
 
 
 
 
Brian
Guest
Posts: n/a
 
      12-17-2006
>
> strCSVFile.replace( / /g, '');
> strCSVFile.replace( / \n/g, '');
> strCSVFile.replace( / \r/g, '');
>
>


hmmm, very late night typing.. I meant

strCSVFile.replace( / +/g, '');
strCSVFile.replace( /\n+/g, '');
strCSVFile.replace( /\r+/g, '');

 
Reply With Quote
 
mick white
Guest
Posts: n/a
 
      12-17-2006
Brian wrote:

>
> hmmm, very late night typing.. I meant
>
> strCSVFile.replace( / +/g, '');
> strCSVFile.replace( /\n+/g, '');
> strCSVFile.replace( /\r+/g, '');
>

Perhaps you need:
someVariable=strCSVFile.replace(/\s/g,'');

No need for "+", since you are using the "g" modifier. And why not
assign the result of the statement to a variable? (Unless you want to
destroy the original string.)
Mick
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      12-17-2006

Brian wrote:
> I have been working on a data reception system.. I am still finding my
> way around Javascript, though I am accomplishing much.
>
> I just fixed a flaw that was really hard to find. The symptoms are
> this:
>
> I get a multiline string returned to Javascript from a Proxy+Google
> Maps API GDownloadUrl()
> The data, when added to a DOM table looked fine, about 20 lines in CSV
> format
>
> Sunrise,-119.098,35.345,0.0<br>
> SwanDancer,-119.345,35.567,1.0<br>
> ... etc


Try something like:

var strCSVFile = 'Sunrise,-119.098,35.345,0.0<br>'
+ 'SwanDancer,-119.345,35.567,1.0<br>';

/* Remove any leading or trailing br elements */
strCSVFile = strCSVFile.replace(/(^<br>)|(<br>$)/g,'');

var arrayCSVFile = strCSVFile.split('<br>');
var recordElement;

/* arrayCSVFile is now an array with two elements:
*
* ['Sunrise,-119.098,35.345,0.0',
* 'SwanDancer,-119.345,35.567,1.0']
*/
for (var i=0, len=arrayCSVFile.length; i<len; i++){
arrayCSVFile[i] = arrayCSVFile[i].split(',');

/* arrayCSVFile is still an array with two elements,
* but each is now an array of 4 elements:
*
* [
* ['Sunrise', '-119.098', '35.345', '0.0'],
* ['SwanDancer', '-119.345', '35.567', '1.0']
* ]
*/
for (var j=0, len2=arrayCSVFile[i].length; j<len2; j++){
recordElement = arrayCSVFile[i][j];

/* recordElement will be each element in turn, i.e.
* 'Sunrise', then '-119.098', then '35.345', and so on
*/

alert('Record: ' + (i+1) + ' of ' + len
+ '\nElement: ' + (j+1) + ' of ' + len2
+ '\nValue: ' + recordElement);
}
}

If you want to remove all whitespace (all spaces, tabs, linefeeds,
returns, the lot) than add .replace(/\s/g, '') to the end of the line
where the leading and trailing br's are replaced.


--
Rob

 
Reply With Quote
 
Brian
Guest
Posts: n/a
 
      12-18-2006

RobG wrote:
> Brian wrote:...
> Try something like:
>
> var strCSVFile = 'Sunrise,-119.098,35.345,0.0<br>'
> + 'SwanDancer,-119.345,35.567,1.0<br>';
>
> /* Remove any leading or trailing br elements */
> strCSVFile = strCSVFile.replace(/(^<br>)|(<br>$)/g,'');
>
> var arrayCSVFile = strCSVFile.split('<br>');
> var recordElement;
>
> /* arrayCSVFile is now an array with two elements:
> *
> * ['Sunrise,-119.098,35.345,0.0',
> * 'SwanDancer,-119.345,35.567,1.0']
> */
> for (var i=0, len=arrayCSVFile.length; i<len; i++){
> arrayCSVFile[i] = arrayCSVFile[i].split(',');
>
> /* arrayCSVFile is still an array with two elements,
> * but each is now an array of 4 elements:
> *
> * [
> * ['Sunrise', '-119.098', '35.345', '0.0'],
> * ['SwanDancer', '-119.345', '35.567', '1.0']
> * ]
> */
> for (var j=0, len2=arrayCSVFile[i].length; j<len2; j++){
> recordElement = arrayCSVFile[i][j];
>
> /* recordElement will be each element in turn, i.e.
> * 'Sunrise', then '-119.098', then '35.345', and so on
> */
>
> alert('Record: ' + (i+1) + ' of ' + len
> + '\nElement: ' + (j+1) + ' of ' + len2
> + '\nValue: ' + recordElement);
> }
> }
>



hey, Rob, I think you have dome something like this before (!) The
clarity of your formatting alone is helpful. I, also, split twice to
end up with CSV[][].

I ended up going a different direction at the end though. Rather than
make a single, flat line of elements, why not use the JS Object/perl
hash/ObjectiveC collection(?)/STL Map idiom. that is, and array of
objects, a series of name/value pairs. years ago I called it a
dictionary, or associative array (name/value pairs), but didn't use it
much. These days, it seems to have sprung into popularity, with
language support to just 'toss in' elements as needed (though I am not
using it that way here)

So the final lines of the my version turns into:
// Obj w/named fields, the result of processing the CSV
var csvDataObj = {};
csvDataObj.recArray = [];

// process each record
for ( index = 0; index < arrayCSVFile.length; index++ )
{
arrayCSVFile[ index ] = arrayCSVFile[ index ].split( ',' );

// build data container to pass out to other processes
var tRecObj = {};
tRecObj.recName = arrayCSVFile[index][0];
tRecObj.recLat = arrayCSVFile[index][1];
tRecObj.recLng = arrayCSVFile[index][2];
tRecObj.recAlt = arrayCSVFile[index][3];

csvDataObj.recArray.push( tRecObj);
}

// Now do something with arrayCSVFile[][]
gMyCore.processAllDevices( csvDataObj);

On the other end, I could "discover" the elements, but since its only
Javascript I use direct knowledge of the contents in the code.

I wrote it, worked the first time. So some things are going ok

 
Reply With Quote
 
Brian
Guest
Posts: n/a
 
      12-18-2006

mick white wrote:
> Brian wrote:
> >
> > strCSVFile.replace( / +/g, '');
> > strCSVFile.replace( /\n+/g, '');
> > strCSVFile.replace( /\r+/g, '');
> >

> Perhaps you need:
> someVariable=strCSVFile.replace(/\s/g,'');
>
> No need for "+", since you are using the "g" modifier. And why not
> assign the result of the statement to a variable? (Unless you want to
> destroy the original string.)


I would happily destry the original string! but, I am reticent to
admit, those simple replace's were failing silently. Haven't revisited
it quite yet.. on a deadline.. but will look soon

 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      12-18-2006
In comp.lang.javascript message
<erehh.4447$(E-Mail Removed)>, Sun, 17 Dec 2006 16:41:46,
mick white <(E-Mail Removed)> wrote:
>Brian wrote:
>
>> hmmm, very late night typing.. I meant
>> strCSVFile.replace( / +/g, '');
>> strCSVFile.replace( /\n+/g, '');
>> strCSVFile.replace( /\r+/g, '');
>>

> Perhaps you need:
>someVariable=strCSVFile.replace(/\s/g,'');
>
>No need for "+", since you are using the "g" modifier. And why not
>assign the result of the statement to a variable? (Unless you want to
>destroy the original string.)



Str.replace should have no effect on Str's existence. The .replace
method generates a new string, but does not destroy the old one.

AFAIK, the only type of Object which has Methods provided to change its
value is the Date Object.


I would expect, if there are instances of multiple /r, for the /r+
version to be slightly faster, since it calls for fewer replacements.
That could be implementation-dependent. A quick test shows a slight
gain in speed when using +.

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

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/> A FAQ for news:comp.lang.javascript.
<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.
 
Reply With Quote
 
mick white
Guest
Posts: n/a
 
      12-18-2006
Dr J R Stockton wrote:

> mick white <(E-Mail Removed)> wrote:


>> Perhaps you need:
>> someVariable=strCSVFile.replace(/\s/g,'');
>>
>> No need for "+", since you are using the "g" modifier. And why not
>> assign the result of the statement to a variable? (Unless you want to
>> destroy the original string.)

>
>
> Str.replace should have no effect on Str's existence. The .replace
> method generates a new string, but does not destroy the old one.


var StringA="A B C"
var StringA=StringA.replace(/\s/g,'');
alert(StringA);
But you're correct, /technically/.

Mick

 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      12-19-2006
In comp.lang.javascript message
<jOChh.4643$(E-Mail Removed)>, Mon, 18 Dec 2006 20:24:47,
mick white <(E-Mail Removed)> wrote:
>Dr J R Stockton wrote:


>> Str.replace should have no effect on Str's existence. The .replace
>>method generates a new string, but does not destroy the old one.

>
>var StringA="A B C"
>var StringA=StringA.replace(/\s/g,'');
>alert(StringA);
>But you're correct, /technically/.


It is not the .replace that destroys "A B C", but the subsequent
assignment.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
Xah's Edu Corner: The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee Perl Misc 21 03-21-2006 07:02 AM
Xah's Edu Corner: The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee Python 23 03-21-2006 07:02 AM
Xah's Edu Corner: The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee Java 22 03-21-2006 07:02 AM
how to define a variable to hold a multiline text input in perl from html multiline textbox dale zhang Perl Misc 8 11-30-2004 06:53 AM



Advertisments