/    Sign up×
Community /Pin to ProfileBookmark

Separating Integers Into Groups of Three

Is it possible using Javascript to take a binary string and, working from right to left, extract three digits at a time and pass each into a text field?

For example, this is the binary string for 148573 – 100100010001011101.
I want to separate it like this: 100 (clusterF) 100 (clusterE) 010 (clusterD) 001 (clusterC) 011 (cluster? 101 (clusterA).
Having achieved this I will then prepare a report interpreting what each cluster means, thanks to Valli’s efforts earlier this week.

I already can place commas at three digit intervals using Mark Henwood’s “Adding Commas” routine, but the string remains intact.

Is there some code to put me on the right track? You have been an excellent team so far. I have wanted to do this for so long, but never had the confidence to ask. ?

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@CharlesJul 06.2006 — Try clusterArray = '100100010001011101'.match (/d{3}/g)
Copy linkTweet thisAlerts:
@TimeTrackerauthorJul 07.2006 — Charles, thanks, the only thing the above code did was to place commas at three digit intervals, starting from the left. That is not what I was attempting to do. Below is non-working code, yet it expresses what I want to achieve a little better.

<HTML>

<HEAD>

<script LANGUAGE="Javascript">

<!--

// There are 22 digits in this binary cluster. The first digit is not important. -->

// I am attempting to pass the remaining three digits left after subtraction to the respective text field. -->

function makeClusters(df){

clusterArray = df.binstring.value;

var Int = integer(clusterArray);

var t = Int(1,2,3);

var u = Int(4,5,6);

var v = Int(7,8,9);

var w = Int(10,11,12);

var x = Int(13,14,15);

var y = Int(16,17,18);

var z = Int(19,20,21);

if (i=0; i=0+1; i<=21){

var clusterT = df.binstring.value - Int(u,v,w,x,y,z);

var clusterU = df.binstring.value - Int(t,v,w,x,y,z);

var clusterV = df.binstring.value - Int(t,u,w,x,y,z);

var clusterW = df.binstring.value - Int(t,u,v,x,y,z);

var clusterX = df.binstring.value - Int(t,u,v,w,y,z);

var clusterY = df.binstring.value - Int(t,u,v,w,x,z);

var clusterZ = df.binstring.value - Int(t,u,v,w,x,y);

}

}

// -->

</script>

</HEAD>

<BODY>

<form>

<input type="text" name="binstring" size=25 value="1001000100010111011101">

<br><input type="button" name="clusters" value="Separate" onClick="makeClusters(this.form)">

<br><input type="text" name="clusterT" size=4><input type="text" name="clusterU" size=4>

<br><input type="text" name="clusterV" size=4><input type="text" name="clusterW" size=4>

<br><input type="text" name="clusterX" size=4><input type="text" name="clusterY" size=4>

<br><input type="text" name="clusterZ" size=4>

</form>

</BODY>

</HTML>
Copy linkTweet thisAlerts:
@CharlesJul 07.2006 — Charles, thanks, the only thing the above code did was to place commas at three digit intervals, starting from the left. [/QUOTE]No, what my example did was split your string into an Array by threes. When you use an Array as a String in JavaScript Array.join (',') is called in the background.clusterArray = '100100010001011101'.match (/d{3}/g)
document.write ('&lt;ul&gt;')
i = 0
while (e = clusterArray [i++]) {document.write ('&lt;li&gt;', e, '&lt;/li&gt;')
document.write ('&lt;/ul&gt;')
Copy linkTweet thisAlerts:
@phpnoviceJul 07.2006 — No, what my example did was split your string into an Array by threes.[/QUOTE]
That is left-to-right.
Is it possible using Javascript to take a binary string and, [COLOR=Red]working from right to left[/COLOR], extract three digits at a time and pass each into a text field?[/QUOTE]
Copy linkTweet thisAlerts:
@CharlesJul 07.2006 — String.prototype.reverse = function (){return this.split('').reverse().join('')}
clusterArray = '100100010001011101'.reverse().match (/d{3}/g)
for (i = 0; i &lt; clusterArray.length; i++) {clusterArray[i] = clusterArray[i].reverse()
Copy linkTweet thisAlerts:
@TimeTrackerauthorJul 08.2006 — Charles: The binary sequences in the 4th and 7th clusters from the right appear to be reversed (100 should be 001; 110 should be 011).

This is what the string normally looks like clustered with commas: 1,011,101,000,001,101,000,011.

If you shorten the string or replace it with another, it looks as if all but the mirroring digits in each cluster after the first are reversed.

Is there some way to correct this? Otherwise the code has excellent potential. Thank you.

<HTML>

<HEAD>

<script LANGUAGE="Javascript">

<!--

function makeClusters(df){

var binstring = df.binstring.value;

String.prototype.reverse = function ()

{

return this.split('').reverse().join('')

}

clusterArray = df.binstring.value.reverse().match (/d{3}/g)

for (i = 0; i < clusterArray.length; i++)

{

clusterArray[i] = clusterArray[i].reverse()

i = 0

while (e = clusterArray [i++])

{

df.clusterT.value = clusterArray[0]

df.clusterU.value = clusterArray[1]

df.clusterV.value = clusterArray[2]

df.clusterW.value = clusterArray[3]

df.clusterX.value = clusterArray[4]

df.clusterY.value = clusterArray[5]

df.clusterZ.value = clusterArray[6]

}

}

}

// -->

</script>

</HEAD>

<BODY>

<form><input type="text" name="binstring" size=24 value="1011101000001101000011">

<input type="button" name="clusters" value="Separate" onClick="makeClusters(this.form)">

<br><input type="text" name="clusterZ" size=2><input type="text" name="clusterY" size=2><input type="text" name="clusterX" size=2><input type="text" name="clusterW" size=2><input type="text" name="clusterV" size=2><input type="text" name="clusterU" size=2><input type="text" name="clusterT" size=2>

<br><input type="reset" name="Reset"></form>



</BODY>

</HTML>
Copy linkTweet thisAlerts:
@cridleyJul 08.2006 — Err... huh... Don't blame Charles for your bugs...

What's wrong with the following code? :

for (i = 0; i < clusterArray.length; i++)

{

clusterArray[i] = clusterArray[i].reverse()

i = 0

while (e = clusterArray [i++])

{

df.clusterT.value = clusterArray[0]

df.clusterU.value = clusterArray[1]

df.clusterV.value = clusterArray[2]

df.clusterW.value = clusterArray[3]

df.clusterX.value = clusterArray[4]

df.clusterY.value = clusterArray[5]

df.clusterZ.value = clusterArray[6]

}

}





Answer? Everything. Why is the while nested in the for loop? Why is there a while loop at all?

you're only ever reversing element 0. Rename your boxes to cluster_0, cluster_1 etc. and try something like



for (i = 0; i < clusterArray.length; i++)

{

clusterArray[i] = clusterArray[i].reverse()



eval("df.cluster_"+String(i)+".value = clusterArray["+String(i)+"]")
}
Copy linkTweet thisAlerts:
@TimeTrackerauthorJul 09.2006 — Cridley:

I am not blaming Charles (I am grateful to him), having wondered about the 'while' in the loop myself. He provided that code, not me (see above). I found that the javascript actually works without it - but that was after my last post. Even so, Charles's original bullet list code also reversed the order of the digits in each cluster. I have spent days working on this already and will now happily try your solution. Here's to some success at last. Thanks.
Copy linkTweet thisAlerts:
@TimeTrackerauthorJul 09.2006 — Cridley and Charles, you are both gems. The script was working within five minutes with very little extra effort. You are so good!!!

Yet this opens up another window of opportunity for me. I am going to have a find a way to write and include .js files to handle all the commentary for the report writing which is now possible.
Copy linkTweet thisAlerts:
@cridleyJul 09.2006 — No idea what you have just said, but i'm glad to have been of assistance.
×

Success!

Help @TimeTracker spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...