split into groups of 2?

G

Geoff Cox

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
 
A

ASM

Geoff said:
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+' '+fred[i+1]; j++ }
fred.length = j;
 
G

Geoff Cox

Geoff said:
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+' '+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
 
A

ASM

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+' '+fred[i+1]; j++
}
fred.length = j;

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

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

</script>
</html>
 
A

ASM

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
 
G

Geoff Cox

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+' '+fred[i+1]; j++
}
fred.length = j;

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

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

</script>
</html>
 
A

ASM

Geoff said:
On Wed, 14 Sep 2005 12:26:26 +0200, ASM
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);

// delete first and/or last space if existing in 'fred'

// 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 ==> ' '

// a counter set to zero

// from a value = 0 to a value = precedent array length
// with a variation = to 2

// fred's element of row = j becomes
// fred's element of row = i plus his next fellow
fred[j] = fred+' '+fred[i+1];


// increase of 1 the counter '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

// 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 = fred.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>
 
B

Baconbutty

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);
 
V

Vic Sowers

ASM said:
Geoff said:
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+' '+fred[i+1]; j++ }
fred.length = j;


Also:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top