# Grouping array elements

**URL:** <https://forum.kirupa.com/t/grouping-array-elements/185187>\
**Category:** Uncategorized\
**Created:** [April 11, 2006, 4:49pm UTC](https://forum.kirupa.com/t/grouping-array-elements/185187 "2006-04-11T16:49:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixeldude](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pixeldude](https://forum.kirupa.com/u/pixeldude)\
**Post date:** [April 11, 2006, 4:49pm UTC](https://forum.kirupa.com/t/grouping-array-elements/185187/1 "2006-04-11T16:49:18Z")

</div>

I have a problem kind of lost it…how to grouping the elements in array into 2 section/or put in 2 new array…

Ok the scenario is :I have several data retrieve from dtbase like in this as code

```
 ActionScript Code:
 [FONT=Courier New][LEFT][COLOR=#0000ff]stop[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];

```

[COLOR=#000000] **var** [/COLOR] getCusDt:[COLOR=#0000ff]LoadVars[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000ff]LoadVars[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000] **var** [/COLOR] receCusDt:[COLOR=#0000ff]LoadVars[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000ff]LoadVars[/COLOR]COLOR=#000000[/COLOR];  
receCusDt.[COLOR=#0000ff]onLoad[/COLOR] = loadCusDt;

loadBtn.[COLOR=#0000ff]onPress[/COLOR] = [COLOR=#000000] **function** [/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
getCusDt.[COLOR=#000080]usr\_name[/COLOR] = usrname.[COLOR=#0000ff]text[/COLOR];  
getCusDt.[COLOR=#0000ff]sendAndLoad[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]

[COLOR=#808080]_//////////////////////////////////////_[/COLOR]  
[COLOR=#000000] **function** [/COLOR] loadCusDtCOLOR=#000000[/COLOR][COLOR=#000000]{[/COLOR]  
[COLOR=#0000ff]if[/COLOR]COLOR=#000000[/COLOR][COLOR=#000000]{[/COLOR]  
info.[COLOR=#0000ff]text[/COLOR] = [COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]message[/COLOR];  
[COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i = [COLOR=#000080]0[/COLOR]; i\<this.[COLOR=#000080]items[/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
dtCustomize = [COLOR=#0000ff]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#ff0000]“datacustomize”[/COLOR]+i[COLOR=#000000]][/COLOR];  
plottingCOLOR=#000000[/COLOR];  
usrname.[COLOR=#0000ff]text[/COLOR] = [COLOR=#ff0000]""[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR][COLOR=#0000ff]else[/COLOR][COLOR=#000000]{[/COLOR]  
info.[COLOR=#0000ff]text[/COLOR] = [COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]message[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[COLOR=#808080]_////////////////plotting part////////////_[/COLOR]  
[COLOR=#000000] **function** [/COLOR] plottingCOLOR=#000000[/COLOR][COLOR=#000000]{[/COLOR]  
[COLOR=#808080]_//trace(dt);_[/COLOR]  
[COLOR=#000000] **var** [/COLOR] descriptor:[COLOR=#0000ff]Array[/COLOR] = [COLOR=#000000][[/COLOR][COLOR=#000000]][/COLOR];  
[COLOR=#808080]_//insert into array…_[/COLOR]  
descriptor = dt.[COLOR=#0000ff]split[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#808080]_//okay the problem is here…how to separate the dt to become 2 section 1 for mymc_ and for another 1 is for textholder section…meaning that I can separate into 2 section…_[/COLOR]  
[COLOR=#0000ff]trace[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#808080]_//output --\> ,textholder0_29_26_100_100_fifin04_courier new_oxcc0000_18,mymc0_80_101_241_243\*[/COLOR]

[COLOR=#000000]}[/COLOR]  
[/LEFT]  
[/FONT]

It means I will get the long string like this from the database

```auto
 |textholder0*29*26*100*100*fifin04*courier new*oxcc0000*18|mymc0*80*101*241*243

```

so my question is how to grouping it just like this

```auto

mcArr = [mymc0*80*101*241*243,mymc1*80*101*241*243]

textArr = [textholder0*29*26*100*100*fifin04*courier new*oxcc0000*18,textholder1*29*26*100*100*fifin04*courier new*oxcc0000*18]

```

from the long string…

---

<div class="post-metadata">

**Author:** ![virusescu](https://avatars.discourse-cdn.com/v4/letter/v/8edcca/32.png) [@virusescu](https://forum.kirupa.com/u/virusescu)\
**Post date:** [April 11, 2006, 7:42pm UTC](https://forum.kirupa.com/t/grouping-array-elements/185187/2 "2006-04-11T19:42:05Z")

</div>

You can use the split method of a string to group elements into an array.  
Here’s an example considering your string.

```auto
str = "|textholder0*29*26*100*100*fifin04*courier new*oxcc0000*18|mymc0*80*101*241*243";
myData = new Array();
myData = str.split("|");
for (var x in myData) {
	var tmpStr = myData[x];
	myData[x] = tmpStr.split("*");
}
trace(myData[0][0]);// traces nothing because you have a first | so it's considered that the first element is empty
trace(myData[1][0]);// traces textholder0
trace(myData[2][3]);// traces 241

```

Good luck

PS. After I posted I’ve noticed the comma. You first split using the comma instead of the \* sing and then split the results again using the \* sign. I hope you got the ideea.

---

<div class="post-metadata">

**Author:** ![pixeldude](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pixeldude](https://forum.kirupa.com/u/pixeldude)\
**Post date:** [April 12, 2006, 5:17am UTC](https://forum.kirupa.com/t/grouping-array-elements/185187/3 "2006-04-12T05:17:09Z")

</div>

Hi…virusescu…

1stly thanks for ur reply…ok regarding the split and then put in array that’s 1st thing I done…before I raise up here…okay if I just split the long string and put in 1 array…the problem is…the length of the string will not static…sometime it can be 5/6 and so on…so how would i detect of the array if I just put all in 1 array only…?..anyhow I’ll dig it 1st…then I’ll come back if I have any issue…

Edit:so if I can categorize the elements in 2 categorize like

```auto

mcArr = [mymc0*80*101*241*243,mymc1*80*101*241*243,mymc2*80*101*241*243]

txtArr = [textholder0*29*26*100*100*fifin04*courier new*oxcc0000*18,textholder1*29*26*100*100*fifin04*courier new*oxcc0000*18]

```

so I’ll easily loop the array for attaching MC purposing…u know what I mean mate :devious:

thanks again…

---

<div class="post-metadata">

**Author:** ![pixeldude](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pixeldude](https://forum.kirupa.com/u/pixeldude)\
**Post date:** [April 12, 2006, 6:32am UTC](https://forum.kirupa.com/t/grouping-array-elements/185187/4 "2006-04-12T06:32:32Z")

</div>

> [@pixeldude](#):
>
> Hi…virusescu…
> 
> 1stly thanks for ur reply…ok regarding the split and then put in array that’s 1st thing I done…before I raise up here…okay if I just split the long string and put in 1 array…the problem is…the length of the string will not static…sometime it can be 5/6 and so on…so how would i detect of the array if I just put all in 1 array only…?..anyhow I’ll dig it 1st…then I’ll come back if I have any issue…
> 
> thanks again…

Edit:so if I can categorize the elements in 2 categorize like

```auto

mcArr = [mymc0*80*101*241*243,mymc1*80*101*241*243,mymc2*80*101*241*243]

txtArr = [textholder0*29*26*100*100*fifin04*courier new*oxcc0000*18,textholder1*29*26*100*100*fifin04*courier new*oxcc0000*18]

```

so I’ll easily loop the array for attaching MC purposing…u know what I mean mate :devious:

---

<div class="post-metadata">

**Author:** ![virusescu](https://avatars.discourse-cdn.com/v4/letter/v/8edcca/32.png) [@virusescu](https://forum.kirupa.com/u/virusescu)\
**Post date:** [April 12, 2006, 7:18am UTC](https://forum.kirupa.com/t/grouping-array-elements/185187/5 "2006-04-12T07:18:33Z")

</div>

> so how would i detect of the array if I just put all in 1 array only…?

I really don’t understand your problem/question. It’s called a multidimensionall array - notice the repeated access.

> if I just split the long string and put in 1 array…the problem is…the length of the string will not static…sometime it can be 5/6 and so on

So what? Have you seen any numbers before the traces in my code example :)?

---

<div class="post-metadata">

**Author:** ![pixeldude](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pixeldude](https://forum.kirupa.com/u/pixeldude)\
**Post date:** [April 12, 2006, 8:55am UTC](https://forum.kirupa.com/t/grouping-array-elements/185187/6 "2006-04-12T08:55:53Z")

</div>

> [@virusescu](#):
>
> I really don’t understand your problem/question. It’s called a multidimensionall array - notice the repeated access.
> 
> So what? Have you seen any numbers before the traces in my code example :)?

Hi…sorry for delay reply mate…1stly sorry if my question are still blur to u Ady…ok if I’m using ur method it’ll only trace individual elements array eg;

```auto

trace(myData[1][0]);// traces textholder0
trace(myData[2][0]);// traces mymc0
trace(myData[3][0]);// traces textholder1
trace(myData[4][0]);// traces mymc1

```

so which is hard for me to loop the array for attaching mc purposing later on…so what I after is how to put the textholder things into txtArr and mymc things into mcArr that’s all…eg;

```auto

txtArr = [textholder0*29*26*100*100*fifin04*courier new*oxcc0000*18,textholder1*29*26*100*100*fifin04*courier new*oxcc0000*18];

mcArr = [mymc0*80*101*241*243,mymc1*80*101*241*243];

```

so later on I can easily acces the 2 categorize array for attaching plotting mc purposing…hope u get it Ady…🙂

tq again

---

<div class="post-metadata">

**Author:** ![virusescu](https://avatars.discourse-cdn.com/v4/letter/v/8edcca/32.png) [@virusescu](https://forum.kirupa.com/u/virusescu)\
**Post date:** [April 12, 2006, 9:55am UTC](https://forum.kirupa.com/t/grouping-array-elements/185187/7 "2006-04-12T09:55:04Z")

</div>

> ok if I’m using ur method it’ll only trace individual elements array eg;

That’s the whole point so that you can access individual properties after splitting your data.  
Here’s an example with the data arranged in 2 different arrays.

```auto
str = "textholder0*29*26*100*100*fifin04*courier new*oxcc0000*18|mymc0*80*101*241*243|textholder1*25*23*90*90*fifin04*courier new*oxcc0000*18|mymc1*80*101*241*243";
myData = new Array();
myData = str.split("|");
for (var x in myData) {
	var tmpStr = myData[x];
	myData[x] = tmpStr.split("*");
}
// so now you have all the data arrangedn in the myData array.
// You can further arrange it in 2 separate arrays like this
var mcArr = new Array();
var textArr = new Array();
for (var x in myData) {
	if (myData[x][0].indexOf("text")>=0) {
		// means we have as the first element a string that contains "text";
		// so we put it in the text Arr;
		textArr.push(myData[x].slice());
	} else {
		mcArr.push(myData[x].slice());
	}
}
delete myData;
for (var x in textArr) {
	for (var y in textArr[x]) {
		trace("textArr["+x+"] has y:"+y+" with value "+textArr[x][y]);
	}
}
trace("---------------------");
for (var x in mcArr) {
	for (var y in mcArr[x]) {
		trace("mcArr["+x+"] has y:"+y+" with value "+mcArr[x][y]);
}
}
// this means that you can still trace individual elements easyer.
// if you would need to find out the second property of the first textholder 
// you can easly access it like textArr[0][1]

```

Hope this helps

---

<div class="post-metadata">

**Author:** ![pixeldude](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pixeldude](https://forum.kirupa.com/u/pixeldude)\
**Post date:** [April 13, 2006, 6:24am UTC](https://forum.kirupa.com/t/grouping-array-elements/185187/8 "2006-04-13T06:24:49Z")

</div>

Hi…Ady…

it works now…thanks for ur help…mate…
