469,916 Members | 2,454 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

split into groups of 2?

Hello,

If I have

fred = "0 -10 5 6 ";

how can I split above into groups of 2, ie

0,-10

and

5,6 ?

Cheers

Geoff
Sep 14 '05 #1
8 1934
ASM
Geoff Cox wrote:
Hello,

If I have

fred = "0 -10 5 6 ";

how can I split above into groups of 2, ie


fred = fred.split(' ');
var j=0;
for(var i=0;i<fred.length;i=i+2);
{ fred[j] = fred[i]+' '+fred[i+1]; j++ }
fred.length = j;
--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #2
On Wed, 14 Sep 2005 11:40:07 +0200, ASM
<st*********************@wanadoo.fr.invalid> wrote:
Geoff Cox wrote:
Hello,

If I have

fred = "0 -10 5 6 ";

how can I split above into groups of 2, ie


fred = fred.split(' ');
var j=0;
for(var i=0;i<fred.length;i=i+2);
{ fred[j] = fred[i]+' '+fred[i+1]; j++ }
fred.length = j;


Stephane,

Can you please explain what is happening? I obviously don't follow
what you have done as I get undefined when I use

alert(fred[0]);

after the above.

Cheers

Geoff
Sep 14 '05 #3
ASM
ASM wrote:
Geoff Cox wrote:
Hello,

If I have

fred = "0 -10 5 6 ";

how can I split above into groups of 2, ie


complements and corrections :

<html>
<script type="text/javascript">

fred = " 0 -10 5 6 ";

if(fred.lastIndexOf(' ')==fred.length-1)
fred = fred.substring(0,fred.length-1);
if(fred.indexOf(' ')==0)
fred = fred.substring(1);

// general array
fred = fred.split(' ');
var j=0;
for(var i=0;i<fred.length-1;i+=2) {
fred[j] = fred[i]+' '+fred[i+1]; j++
}
fred.length = j;

// secondary array
for(var i=0;i<fred.length;i++) {
fred[i] = fred[i].split(' ');

// verification
alert('array = '+fred+'\nfred[0][1] = '+fred[0][1]);

</script>
</html>

--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #4
ASM
Geoff Cox wrote:
On Wed, 14 Sep 2005 11:40:07 +0200, ASM
<st*********************@wanadoo.fr.invalid> wrote:

Geoff Cox wrote:
Hello,

If I have

fred = "0 -10 5 6 ";

how can I split above into groups of 2, ie
fred = fred.split(' ');


Stephane,

Can you please explain what is happening? I obviously don't follow
what you have done as I get undefined when I use


see corrections and complements send by otherway

--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #5
On Wed, 14 Sep 2005 12:26:26 +0200, ASM
<st*********************@wanadoo.fr.invalid> wrote:
complements and corrections :
Thanks for this Stephane - I have it working but perhaps you could
explain hwo it works!?

Geoff

<html>
<script type="text/javascript">

fred = " 0 -10 5 6 ";

if(fred.lastIndexOf(' ')==fred.length-1)
fred = fred.substring(0,fred.length-1);
if(fred.indexOf(' ')==0)
fred = fred.substring(1);

// general array
fred = fred.split(' ');
var j=0;
for(var i=0;i<fred.length-1;i+=2) {
fred[j] = fred[i]+' '+fred[i+1]; j++
}
fred.length = j;

// secondary array
for(var i=0;i<fred.length;i++) {
fred[i] = fred[i].split(' ');

// verification
alert('array = '+fred+'\nfred[0][1] = '+fred[0][1]);

</script>
</html>


Sep 14 '05 #6
ASM
Geoff Cox wrote:
On Wed, 14 Sep 2005 12:26:26 +0200, ASM
<st*********************@wanadoo.fr.invalid> wrote:
Geoff
Thanks for this Stephane - I have it working but perhaps you could
explain hwo it works!?


it works with Javascript

what don't you understand ?

var S = 'stephane';
// position of 'n' in S :
p = S.indexOf('n');
// position of 'e' in S :
p1 = S.indexOf('e');
// position of 'e' in S :
p2 = S.lastIndexOf('e');

alert('position of :\n n = '+p+'\n1st e = '+p1+'\n2nd e = '+p2);

// extract 'ne' from S :
str1 = S.substring(p);
// extract 'ste' from S :
str2 = S.substring(0, p1+1);
// extract 'phane' from S :
str3 = S.substring(p1+1);

/* stringToGet = originalString.substring(i_1, i_2)
*
* i_1 = index in originalString ==> will be 1st of stringToGet
* i_2 = index in originalString ==> will be last of stringToGet
* if i_2 is obmited, stringToGet finishes with end of originalString
*/

alert('extractions :\n ne = '+str1+'\n ste = '+str2+'\nphane = '+str3);
<html>
<script type="text/javascript">

fred = " 0 -10 5 6 ";
// delete first and/or last space if existing in 'fred'
if(fred.lastIndexOf(' ')==fred.length-1)
fred = fred.substring(0,fred.length-1);
if(fred.indexOf(' ')==0)
fred = fred.substring(1);

// general array
// create array with each indidual number from 'fred'
// using function : split(separator)
// will troncatenate the string with bits separed by separator
// Here, separator is a white space ==> ' '
fred = fred.split(' ');
// a counter set to zero
var j=0;
// from a value = 0 to a value = precedent array length
// with a variation = to 2
for(var i=0;i<fred.length-1;i+=2) {
// fred's element of row = j becomes
// fred's element of row = i plus his next fellow
fred[j] = fred[i]+' '+fred[i+1];
// increase of 1 the counter 'j'
j++
}
// and rebegin with new values of 'j' and of 'i'

// because fred is now less large
// we tell him how long it is
// (we know this length which was given by last value of 'j')
// exactly same as deleting last survivors old elements
fred.length = j;
// we did get fred = ('0 -10' , '5 6')
// each element of fred will now be broken in 2 parts
// that's to say we create a secondary array for each element
// secondary array
for(var i=0;i<fred.length;i++) {
fred[i] = fred[i].split(' ');
/* fred is now composed with :
* fred[0][0] = '0';
* fred[0][1] = '-10';
* fred[1][0] = '5';
* fred[1][0] = '6';
*
*/
// verification
alert('array = '+fred+'\nfred[0][1] = '+fred[0][1]);

</script>
</html>


--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #7
Try also:-
var s=" 0 -10 2 6 ";
var r=/\s*([0-9.-]+)\s+([0-9.-]+)\s*/g;
var s=s.replace(r,"$1,$2:");
var a=s.split(":").slice(0,-1);

Sep 15 '05 #8

"ASM" <st*********************@wanadoo.fr.invalid> wrote in message
news:43***********************@news.wanadoo.fr...
Geoff Cox wrote:
Hello,

If I have

fred = "0 -10 5 6 ";

how can I split above into groups of 2, ie


fred = fred.split(' ');
var j=0;
for(var i=0;i<fred.length;i=i+2);
{ fred[j] = fred[i]+' '+fred[i+1]; j++ }
fred.length = j;
--
Stephane Moriaux et son [moins] vieux Mac


Also:

var fred = "0 -10 5 6", mary = [];
fred.replace(/\d+(?:\D+\d+)?/g,function ($1) {mary.push($1)});

Sep 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by William Stacey [MVP] | last post: by
7 posts views Thread by byoxemeek | last post: by
7 posts views Thread by lgbjr | last post: by
4 posts views Thread by michael.bollhoefer | last post: by
7 posts views Thread by David | last post: by
7 posts views Thread by Jordi Rico | last post: by
1 post views Thread by Query Builder | last post: by
3 posts views Thread by Rajanikanth Jammalamadaka | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.