473,785 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiline string split problem and fix

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.spli t( "<br>" );

for ( index = 0; index < arrayCSVFile.le ngth; 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|6 8|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

Dec 17 '06 #1
11 5085
btw- I just added

strCSVFile.repl ace( / /g, '');
strCSVFile.repl ace( / \n/g, '');
strCSVFile.repl ace( / \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...

Dec 17 '06 #2
>
strCSVFile.repl ace( / /g, '');
strCSVFile.repl ace( / \n/g, '');
strCSVFile.repl ace( / \r/g, '');

hmmm, very late night typing.. I meant

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

Dec 17 '06 #3
Brian wrote:
>
hmmm, very late night typing.. I meant

strCSVFile.repl ace( / +/g, '');
strCSVFile.repl ace( /\n+/g, '');
strCSVFile.repl ace( /\r+/g, '');
Perhaps you need:
someVariable=st rCSVFile.replac e(/\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
Dec 17 '06 #4

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.repl ace(/(^<br>)|(<br>$)/g,'');

var arrayCSVFile = strCSVFile.spli t('<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=arrayCSVFil e.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=arrayCSVFi le[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

Dec 17 '06 #5

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.repl ace(/(^<br>)|(<br>$)/g,'');

var arrayCSVFile = strCSVFile.spli t('<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=arrayCSVFil e.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=arrayCSVFi le[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.recA rray = [];

// process each record
for ( index = 0; index < arrayCSVFile.le ngth; 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.recA rray.push( tRecObj);
}

// Now do something with arrayCSVFile[][]
gMyCore.process AllDevices( 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 :-)

Dec 18 '06 #6

mick white wrote:
Brian wrote:

strCSVFile.repl ace( / +/g, '');
strCSVFile.repl ace( /\n+/g, '');
strCSVFile.repl ace( /\r+/g, '');
Perhaps you need:
someVariable=st rCSVFile.replac e(/\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

Dec 18 '06 #7
In comp.lang.javas cript message
<er************ ****@twister.ny roc.rr.com>, Sun, 17 Dec 2006 16:41:46,
mick white <mi**@mickweb.c omwrote:
>Brian wrote:
> hmmm, very late night typing.. I meant
strCSVFile.repl ace( / +/g, '');
strCSVFile.repl ace( /\n+/g, '');
strCSVFile.repl ace( /\r+/g, '');
Perhaps you need:
someVariable=s trCSVFile.repla ce(/\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.c om/faq/ A FAQ for news:comp.lang. javascript.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 18 '06 #8
Dr J R Stockton wrote:
mick white <mi**@mickweb.c omwrote:
>Perhaps you need:
someVariable=s trCSVFile.repla ce(/\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

Dec 18 '06 #9
In comp.lang.javas cript message
<jO************ ****@twister.ny roc.rr.com>, Mon, 18 Dec 2006 20:24:47,
mick white <mi**@mickweb.c omwrote:
>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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1958
by: Rasmus Fogh | last post by:
Dear All, I need a way of writing strings or arbitrary Python code that will a) allow the strings to be read again unchanged (like repr) b) write multiline strings as multiline strings instead of escaping the \n's. A repr function that output triple-quoted strings with explicit (non-escaped) linebreaks would be perfect.
6
6186
by: Iver Erling Årva | last post by:
I am looking for a way to let the users copy e.g. a multi-line address from a textfile and paste it into a webpage where there is one input field for each address line in such a way that not only the first line is pasted, but instead the program automatically jumps to the next field and put line 2 in there and so on. Can this be done? I tried with onkeydown to check for event.keyCode == 13, but it doesn't seem to work with paste. Brgds
2
2718
by: ryanbreakspear | last post by:
Hi all, Is there an easy way to delete the first line from a multi line text box? I've tried assigning the .Lines to various containers but can't seem to find an easy way to get the data back. I don't want to iterate through them. There must be an easy way! Thanks in advance
3
13789
by: Sale | last post by:
How can i remove specific line from the multiline textbox?
9
3180
by: John Salerno | last post by:
How do you make a single string span multiple lines, but also allow yourself to indent the second (third, etc.) lines so that it lines up where you want it, without causing the newlines and tabs or spaces to be added to the string as well? Example (pretend this is all on one line): self.DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n"http://www.w3.org/TR/html4/strict.dtd">\n\n'
7
45815
by: Milsnips | last post by:
Hi there, Can anyone help me out on this one? i want to read each line entered in a multiline textbox and store it into an array. I cant find any properties for this in the control, the only thing i can think of is to use Text.Split and split the string at each carriage return, Any help appreciated. thanks. Paul
1
1210
by: Madzone | last post by:
Hello I am developing software for a machine, i receive data from machine in this format Now i want to devide this entire string into a string array, How can i do that ? I m writing Code like this
2
4114
by: Steve | last post by:
Hello, I'm new to the asp.net programming world and one of my first projects is to create an entry form for managers to enter in some information about their employees. My form has a multiline text box so the manager can add 1 or more employeess. They have to enter in the information as domain\username. My question is, how can I strip the domain name from the begining of the username? It has to be stored in a seperate column in the...
2
3585
by: Slickuser | last post by:
Hi, I have tried these methods but I never get a new line to show on check box tool tip? Any idea how to fix this? Thanks. string strFin = string.Empty; string str = "a b c d e f g"; string strArray = str.Split(' ');
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.