SearchArray AS3 Functions


/ Published in: ActionScript 3
Save to your folder(s)

This SearchArray class was written by Flanture.
I claim no credit for this.

The class can be downloaded from here ...
http://www.box.net/shared/7fj14higai


Copy this code and paste it in your HTML
  1. package com.blogspot.flanture.arrays
  2. {
  3.  
  4. // search Array as3 functions
  5. // author: http://flanture.blogspot.com
  6. // contact: [email protected]
  7. // January 2010
  8.  
  9. import flash.display.Sprite;
  10.  
  11. public class SearchArray extends Sprite {
  12.  
  13. public function SearchArray() {
  14.  
  15. // example usage
  16.  
  17. var a:Array = [1,2,3,5,3];
  18. var b:Array = [2,4,6,8];
  19.  
  20. search(6, b);
  21. trace("a combined with b: "+combine(a,b));
  22. trace("6 exists in b? "+searchB(6,b));
  23. trace("number of 3 in a? "+searchCount(3,a));
  24. trace("intersection of a and b: "+iSection(a,b));
  25. trace("shuffled b: "+shuffle(b));
  26. }
  27.  
  28. // function search finds element in array and displays result
  29.  
  30. public static function search(word:Object, arr:Array):void {
  31. var exists:Boolean = false;
  32. for(var i:uint=0; i < arr.length; i++){
  33. if(arr[i]==word){
  34. trace("Element exist on position "+i);
  35. exists = true;
  36. }
  37. }
  38. if(!(exists)){
  39. trace("Element doesn't exist in array.");
  40. }
  41. }
  42.  
  43. // function searchB finds element in array and returns Boolean value
  44.  
  45. public static function searchB(word:Object, arr:Array):Boolean {
  46. var exists:Boolean = false;
  47. for(var i:uint=0; i < arr.length; i++) {
  48. if(arr[i]==word){
  49. exists = true;
  50. }
  51. }
  52. return exists;
  53. }
  54.  
  55. // function searchCount returns number of apperances of element in array
  56.  
  57. public static function searchCount(word:Object, arr:Array):uint {
  58. var counter:uint = 0;
  59. for(var i:uint=0; i < arr.length; i++) {
  60. if(arr[i]==word){
  61. counter+=1;
  62. }
  63. }
  64. return counter;
  65. }
  66.  
  67. // function iSection returns intersection array of two arrays
  68.  
  69. public static function iSection(arr1:Array, arr2:Array):Array {
  70. var arr3:Array = new Array();
  71. var count:uint = 0;
  72. for(var i:uint=0; i < arr1.length; i++){
  73. for(var j:uint=0; j < arr2.length; j++){
  74. if(arr1[i]==arr2[j]){
  75. arr3[count] = arr1[i];
  76. count+=1;
  77. }
  78. }
  79. }
  80. return arr3;
  81. }
  82.  
  83. // function shuffle simply shuffles given array elements
  84.  
  85. public static function shuffle(b:Array):Array {
  86. var temp:Array = new Array();
  87. var templen:uint;
  88. var take:uint;
  89. while (b.length > 0) {
  90. take = Math.floor(Math.random()*b.length);
  91. templen = temp.push(b[take]);
  92. b.splice(take,1);
  93. }
  94. return temp;
  95. }
  96.  
  97. // function combine returns union of two arrays
  98.  
  99. public static function combine(ar1:Array, ar2:Array):Array {
  100. var rAr:Array = new Array();
  101. var i:uint = 0;
  102. var j:uint = 0;
  103. while((i < ar1.length) || (j < ar2.length)) {
  104. if(i < ar1.length){
  105. rAr.push(ar1[i]);
  106. i+=1;
  107. }
  108. if(j < ar2.length){
  109. rAr.push(ar2[j]);
  110. j+=1;
  111. }
  112. }
  113. return rAr;
  114. }
  115. }
  116. }

URL: http://flanture.blogspot.com/2010/01/searcharray-as3-functions.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.