Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Comparing Array Element

Guest
May 08, 2014 May 08, 2014

I have this array

var MyArray:Array;

var ABCD:Array;

var Blu:Array;

var Red:Array;

var Black:Array;

var Pink:Array;

MyArray = [1,2,3,4,5,6,7,10,11,12];

ABCD = ["A","B","C","D"] // A: 1,2,3,4,5,6,7,10,11,12 B: 1,2,3,4,5,6,7,10,11,12 C: 1,2,3,4,5,6,7,10,11,12 D: 1,2,3,4,5,6,7,10,11,12

Blu = [14,9,10,1,2,3,12,5,6,7];     // = A

Red = [13,9,10,1,2,3,4,5,6,7];      // = B

Black = [8,9,10,1,2,3,4,5,6,7];     // = C

Pink = [8,9,10,1,2,3,11,5,6,7];     // = D

1-.) and want to leave for a trace like this:

1A
2A
3A
4A
5A.... up 12
1B
2B
3B
4B
5B... up 12
1C
2C
3C
4C

5C ...up 12
1D
2D
3D
4D
5D...up 12

2-.) then compare the position of each element is stored in the array, and compare to see which is greater

would read high to low:

1A // Value = 14

1B // Value = 13

7A // Value = 12

7D // Value = 11

3A // Value = 10

3B // Value = 10

3C // Value = 10

3D // Value = 10

2A  // Value = 9

2B // Value = 9

3C // Value = 9

3D // Value = 9

..... ETC...

@

if there is another way to organize arrays please show me the code or improve

Thnks All!

TOPICS
ActionScript
459
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 09, 2014 May 09, 2014

You don't show what code you are using that is failing for you. I would use something like this:

for(var a in ABCD) {

  for(var b in MyArray) {

  var c:* = MyArray+ABCD;

  trace(c);

  }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 10, 2014 May 10, 2014

Thanks for responding! I did not understand very well your code, and my example is somewhat understandable why'll post again as I could solve several problems that I had presented earlier, I read more about arrays, the example will place me is an array of Poker is not the same because it does not look the same objective.

I hope it's a little easier to visualize.

Example:

var Cards:Array;

var CardsValue:Array;

var CardType:Array;

CardType = ["Diamond","Spade","Heart","Club"];

Cards =

[[2,   3,  4,  5,  6,  7,  8,  9, 10, "J", "Q", "K", "AS"], //Diamond

[2,   3,  4,  5,  6,  7,  8,  9, 10, "J", "Q", "K", "AS"], //Spade

[2,   3,  4,  5,  6,  7,  8,  9, 10, "J", "Q", "K", "AS"], //Heart

[2,   3,  4,  5,  6,  7,  8,  9, 10, "J", "Q", "K", "AS"]] //Club

CardsValue =

[[40, 41, 42, 43, 44, 45, 46, 47, 48,  49,  50,  51,   52], //Diamond

[27, 28, 29, 30, 31, 32, 33, 34, 35,  36,  37,  38,   39], //Spade

[14, 15, 16, 17, 18, 19, 20, 21, 22,  23,  24,  25,   26], //Heart

[ 1,  2,  3,  4,  5,  6,  7,  8,  9,  10,  11,  12,   13]] //Club

each element of the array is unique in its comparison to the value of each letter,

I want to achieve is:

right through to compare one for each element of the array CardsValue, and to order it with the method of the bubble and print a trace

//output:

Card: ASS Value: 52 TypeCard: Diamond

Card: K Value: 51 TypeCard: Diamond

Card: Q Value: 50 TypeCard: Diamond

Card: J Value: 49 TypeCard: Diamond

Card: 10 Value: 48 TypeCard: Diamond

Card: 9 Value: 47 TypeCard: Diamond

........

Card:3 Value: 2 TypeCard: Club

Card:2 Value: 1 TypeCard: Club

Finish.


or do you recommend me?

sinious

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 11, 2014 May 11, 2014

for (var i:Number = 0; i<CardsValue.length-1; i++)

{

  for (var j:Number = 0; j<CardsValue.length-1; j++)

  {

  if(CardsValue[0]>CardsValue[j+1][0])

  {

  aux = CardsValue[j+1];

             CardsValue[j+1] = CardsValue;

             CardsValue = aux;

  }

  }

}

for (var i:Number = 0; i<=CardsValue.length; i++)

{

  trace(CardsValue);

}

.

Scene 1, Layer 'Layer 1', Frame 1, Line 39 1151: A conflict exists with definition i the internal namespace

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 11, 2014 May 11, 2014

I have no idea what you want to achieve in your first reply. What is a bubble? If you want to compare the value of items in each of those arrays in the CardsValue array you will need to use values that can be compared. You may not want to use an array of arrays. If, for instance, you use the arrays in the array Cards as individual arrays, then you can compare the items in array.

The error that shows in your second reply is because you are defining "i" as a variable twice. Once in line 1 and again in line 21. You could change line 21 to this:


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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 11, 2014 May 11, 2014

sorry for the delay, but it took me a bit to migrate from c + + code has as3,

the method Bubble Sort, is a method I learned in c + + to sort an array without a function. I leave here a guide if you did not know.

BUBBLE SORT (Java, C++) | Algorithms and Data Structures

var aux:Number = 0;

var n:Array;

n = [10,4,1,5,8,3,7,0,8,2,9,6,11,0,0,-1,12,-2,-3,-4];

  trace(": " + n);

  trace("------------------");

for (var i:Number = 0; i<n.length; i++)

{

  for (var j:Number = 0; j<n.length; j++)

  {

  if (n>n[j+1])

  {

  aux = n;

  n = n[j+1];

  n[j+1] = aux;

  }

  }

}

for (var k:Number = 0; k<n.length; k++)

{

trace(n);

}

Original Array: 10,4,1,5,8,3,7,0,8,2,9,6,11,0,0,-1,12,-2,-3,-4

descending order:

-4,-3,-2,-1,0,0,0,1,2,3,4,5,6,7,8,8,9,10,11,12

ascending order

12,11,10,9,8,8,7,6,5,4,3,2,1,0,0,0,-1,-2,-3,-4


Example when finished I will post the two-dimensional array

thanks for replying!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 16, 2014 May 16, 2014

var n:Array;

var aux:Number = 0;

// Array

n =

[[2,1,3,4],

[5,8,6,7],

[8,9,0,-1]

];

trace("          0 1 2 3");

trace("        0[x,x,x,x]");

trace("        1[x,x,x,x]");

trace("        2[x,x,x,x]");

  trace("------------------------");

  trace("-----Original Array-----");

for(var a:Number = 0; a<n.length; a++)

{

trace("         [" + n + "]");

}

trace("------------------------");

trace("Size array: " + n.length);

trace("------------------------");

trace("Position of each element: ");

trace("Position 0,0:" + " " + n[0][0]);

trace("Position 0,1:" + " " + n[0][1]);

trace("Position 0,2:" + " " + n[0][2]);

trace("Position 0,3:" + " " + n[0][3]);

trace("Position 1,0:" + " " + n[1][0]);

trace("Position 1,1:" + " " + n[1][1]);

trace("Position 1,2:" + " " + n[1][2]);

trace("Position 1,3:" + " " + n[1][3]);

trace("Position 2,0:" + " " + n[2][0]);

trace("Position 2,1:" + " " + n[2][1]);

trace("Position 2,2:" + " " + n[2][2]);

trace("Position 2,3:" + " " + n[2][3]);

trace("------------------------");

trace(" ---Ordered array--- ");

for(var i:Number = 0; i<n.length; i++)

{

  for(var j:Number = 0; j<n.length; j++)

  {

  for(var k:Number = 0; k<n.length; k++)

  {

  for(var l:Number = 0; l<n.length; l++)

  {

  if(n>n)

  {

  aux = n;

  n = n;

  n = aux;

  }

  }

  }

  }

}

for(var q:Number = 0; q<n.length; q++)

{

  for(var w:Number = 0; w<n.length; w++)

  {

  trace(n);

  }

}

the problem is as follows:

I is not taking the last element of each row I also prints the trace

why?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2014 May 16, 2014
LATEST

While I'm still not sure what it is that you want to achieve here, if you are asking about this section:

for(var q:Number = 0; q<n.length; q++) 

  for(var w:Number = 0; w<n.length; w++) 

  { 

  trace(n); 

  } 


n is the outer array. It has 3 items. Each of the inner arrays has 4 items. You are using the number of items in the outer array as your counter. So, for instance, q<n.length, is going to iterate from 0,1,2 and never reach the last tiem in the inner array.

Does that help?


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines