|
-
 Originally Posted by marlopax
PHP Code:
var my_array:Array = new Array("a","c","b","n","b","v","b","b","d","a","s","c","a","b","b","a","s","a","b","c","b","a","b","a");
function count_by_search(data_arr:Array, str:String) {
Array.prototype.copy = Array.prototype.slice;
var arr = data_arr.copy();
var i = 0;
var count = 0;
while (i<arr.length) {
var j = 0;
while (j<arr.length) {
if (arr[j] == str) {
arr.splice(j,1);
count++;
}
j++;
}
i++;
}
if (count>0) {
return "Duplicate found "+str+": "+(count);
} else if (count == 0) {
return "Invalid search";
} else {
return "No duplicate found";
}
}
Array.prototype.count_by_search = function(str) {
Array.prototype.copy = Array.prototype.slice;
var arr = this.copy();
var i = 0;
var count = 0;
while (i<arr.length) {
var j = 0;
while (j<arr.length) {
if (arr[j] == str) {
arr.splice(j,1);
count++;
}
j++;
}
i++;
}
if (count>0) {
return "Duplicate found "+str+": "+(count);
} else if (count == 0) {
return "Invalid search";
} else {
return "No duplicate found";
}
};
var count;
trace("my_array.count_by_search(\"blah\");\n");
count = my_array.count_by_search("v");
trace(count);
count = my_array.count_by_search("a");
trace(count);
count = my_array.count_by_search("l");
trace(count);
trace("\ncount_by_search(my_array, \"blah\");\n");
count = count_by_search(my_array, "a");
trace(count);
count = count_by_search(my_array, "b");
trace(count);
count = count_by_search(my_array, "l");
trace(count);
Instead doing copy/paste of my code, i suggest you to go through it to understand what the code is doing.
I hope you are going to search the number of similar entries and not the duplicate. Resulting duplicate 1 means you have two similar entries. No duplicate means you have only one entry and Invalid data means no such entries found.
marlopax
But i do understand what the code is doing.. Don't assume things.
If the array is:
var my_array:Array = new Array("a","a","a","a","a","a","a","a","a");
Your code returns 7. Not 9.
var my_array:Array = new Array("a","b","a","a","a","a","a","a","a");
This also returns 7 As. Including the 1 B.
var my_array:Array = new Array("a","b","a","a","a","a","a","a","a","b");
This returns 8 As and 2 Bs. Which is correct.
var my_array:Array = new Array("a","a","a","a","a","a","a","a","v","a","a", "a","a","a","a","a");
This returns 14 As and 1 V. There are 15 As here.
var my_array:Array = new Array("a","a","a","b","a","a","a","a","a","v","a", "a","a","a","a","a","a");
This returns 15 As, 1 V and 1 B. Still, 15 As.
I don't see a problem in the code so i don't understand what is causing this.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|