arrays

What's the best way to count keywords in JavaScript?

What's the best and most efficient way to count keywords in JavaScript? Basically, I'd like to take a string and get the top N words or phrases that occur in the string, mainly for the use of suggesting tags. I'm looking more for conceptual hints or links to real-life examples than actual code, but I certainly wouldn't mind if you'd like...

Best way to find an item in a JavaScript Array ?

What is the best way to find if an object is in an array ? This is the best way I know: function include(arr, obj) { for(var i=0; i<arr.length; i++) { if (arr[i] == obj) return true; } } include([1,2,3,4], 3); // true include([1,2,3,4], 6); // undefined ...

Help with Sparse Array in JScript

I have a sparse array in Jscript, with non-null elements occuring at both negative and positive indices. When I try to use a for in loop, it doesn't traverse the array from the lowest (negative) index to the highest positive index. Instead it returns the array in the order that I added the elements. Enumeration doesn't work either. Is th...

Java: Arrays & Vectors

Hi guys, I'm used to working with PHP but lately I've been working with Java and I'm having a headache trying to figure this out. I want to save this representation in Java: Array ( ["col_name_1"] => Array ( 1 => ["col_value_1"], 2 => ["col_value_2"], ...

Checking if array is multidimensional or not?

What is the most efficient way to check if an array is a flat array of primitive values or if it is a multidimensional array? Is there any way to do this without actually looping through an array and running is_array() on each of its elements? ...

PHP's SPL: Do its interfaces involving arrays cover all array properties?

Would it be possible to write a class that is virtually indistinguishable from an actual PHP array by implementing all the necessary SPL interfaces? Are they missing anything that would be critical? I'd like to build a more advanced Array object, but I want to make sure I wouldn't break an existing app that uses arrays everywhere if I s...

Efficiently merge string arrays in .NET, keeping distinct values

I'm using .NET 3.5. I have two string arrays, which may share one or more values: string[] list1 = new string[] { "apple", "orange", "banana" }; string[] list2 = new string[] { "banana", "pear", "grape" }; I'd like a way to merge them into one array with no duplicate values: { "apple", "orange", "banana", "pear", "grape" } I can d...

Odd compile error in C: creating arrays

I'm trying to figure out what the compiler doesn't like about this: enum { COLS = 10 }; void process_row( int arr2d[][COLS], int row, int arr[], int pickrow); ... int a2d[][COLS] = { {1,2,3}, {4,5,6}, {7,8,9} }; I get a error: ISO C90 forbids mixed declarations and code on the a2d[][COLS] declaration. I tried looking up the error but...

Problem with vector inside a class

Hello, I have this code inside a class: void SendStones() { int currenthole = hole; int lastplace = 0; for(int i=0;i<stns.size();i++) { while(1) {//Calculate new currenthole if(currenthole == 13) { currenthole = 7; break;} if(currenthole == 14) { currenthole = 6; break;} ...

Does an empty array in .NET use any space?

I have some code where I'm returning an array of objects. Here's a simplified example: string[] GetTheStuff() { List<string> s = null; if( somePredicate() ) { s = new List<string>(); // imagine we load some data or something } return (s == null) ? new string[0] : s.ToArray(); } The question is...

Good database design for recall and comparison of 2D data arrays?

I am looking to store 2D arrays of 900x100 elements in a database. Efficient recall and comparison of the arrays is important. I could use a table with a schema like [A, x, y, A(x,y)] such that a single array would compromise 90,000 records. This seems like an ~ok~ table design to store the array, and would provide for efficient recal...

Why do you need $ when accessing array and hash elements in Perl?

Since arrays and hashes can only contain scalars in Perl, why do you have to use the $ to tell the interpreter that the value is a scalar when accessing array or hash elements? In other words, assuming you have an array @myarray and a hash %myhash, why do you need to do: $x = $myarray[1]; $y = $myhash{'foo'}; instead of just doing : ...

Circle coordinates to array in Javascript

What's the best way to add the coordinates of a circle to an array in JavaScript? So far I've only been able to do a half circle, but I need a formula that returns the whole circle to two different arrays: xValues and yValues. (I'm trying to get the coordinates so I can animate an object along a path.) Here's what I have so far: circle...

What's the best way to loop through a set of elements in JavaScript?

In the past and with most my current projects I tend to use a for loop like this: var elements = document.getElementsByTagName('div'); for (var i=0; i<elements.length; i++) { doSomething(elements[i]); } I've heard that using a "reverse while" loop is quicker but I have no real way to confirm this: var elements = document.getEleme...

How to create ArrayList (ArrayList<T>) from array (T[]) in Java

I have an array that is initialised like: Element[] array = {new Element(1),new Element(2),new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList<Element> arraylist = ???; I am sure I have done this before, but the solution is sitting just at the edge of my memory. ...

How do you efficiently generate a list of K non-repeating integers between 0 and an upper bound N

The question gives all necessary data: what is an efficient algorithm to generate a sequence of K non-repeating integers within a given interval. The trivial algorithm (generating random numbers and, before adding them to the sequence, looking them up to see if they were already there) is very expensive if K is large and near enough to N...

Help with array additions in c++

Hi there, I'm not sure if the term's actually "Array Addition". I'm trying to understand what does the following line do: int var[2+1] = {2,1} How is that different from int var[3]? I've been using Java for several years, so I'd appreciate if explained using Java-friendly words ;-) Thanks for your time. -Nushio Edit: Thousands o...

How do I send arrays between views in CakePHP

Hello, I am not sure if I formulated the question right, but still ... I have a view that shows a flash embed and this flash take as parameter a /controller/action URL that generates a XML. I nee to send, from this view, an array to the XML generator action. How is the best way ? Is there some helper->set() method like or I have to cre...

Elegant way to merge two arrays as key value pairs in PHP?

I've got two arrays of the same size. I'd like to merge the two so the values of one are the key indexes of the new array, and the values of the new array are the values of the other. Right now I'm just looping through the arrays and creating the new array manually, but I have a feeling there is a much more elegant way to go about this...

Using Length() with multi-dimensional dynamic arrays in Delphi

I am using a multi-dimensional dynamic array in delphi and am trying to figure this out: I have 2 seperate values for the first index and second index that are totally seperate of each other. As new values come I want to grow the array if that new value is outside of either bound. For new values x, y I check: if Length(List) < (x + ...