arrays

What is the most efficient way to get the first item from an associative array in JavaScript?

I need to get just the first item (actually, just the first key) off a rather large associative array in JavaScript. Here's how I'm doing it currently (using jQuery): getKey = function (data) { var firstKey; $.each(data, function (key, val) { firstKey = key; return false; }); return firstKey; }; Just gu...

Python: can I have a list with named indices?

In PHP I can name my array indicies so that I may have something like: $shows = Array(0 => Array('id' => 1, 'name' => 'Sesaeme Street'), 1 => Array('id' => 2, 'name' => 'Dora The Explorer')); Is this possible in Python? ...

How do I get the unique elements from an array of hashes in Ruby?

I have an array of hashes, and I want the unique values out of it. Calling Array.uniq doesn't give me what I expect. a = [{:a => 1},{:a => 2}, {:a => 1}] a.uniq # => [{:a => 1}, {:a => 2}, {:a => 1}] Where I expected: [{:a => 1}, {:a => 2}] In searching around on the net, I didn't come up with a solution that I was happy with. Fo...

How do I determine if an array is initialized in VB6?

Passing an undimensioned array to the VB6's Ubound function will cause an error, so I want to check if it has been dimensioned yet before attempting to check its upper bound. How do I do this? ...

Efficiently shrinking a two dimensional array in C#

What is an efficient way to shrink a two dimensional array to a smaller size in C#? For example: var bigArray = new object[100, 100]; var smallArray = new object[10, 10]; bigArray[0, 0] = 1; bigArray[0, 1] = 2; ... bigArray[99, 99] = 100000; startRowIndex = 0; startColumnIndex = 0; endRowIndex = 9; endColumnIndex = 9; smallArray = ...

How do I get the key values from $_POST?

echo $_POST["name"]; //returns the value a user typed into the "name" field I would like to be able to also return the text of the key. In this example, I want to return the text "name". Can I do this? ...

Insert array into database in a single row

I wonder if this would be doable ? To insert an array into one field in the database. For instance I have a title, I want to have that title with only one id, but it's going to be bilingually used on the website. It feels a bit unnecessary to make another table to have their global ids and then another table with the actual titles link...

"Arrays" in Scala

In javascript, we can do: ["a string", 10, {x : 1}, function() {}].push("another value"); What is the Scala equivalent? ...

Counting array elements in Python

How can I count the number of elements in an array, because contrary to logic array.count(string) does not count all the elements in the array, it just searches for the number of occurrences of string. ...

In PHP, how do I find the value associated with a specific key.

I have two arrays. One contains id=>count and the other contains id=>name. I'm trying to produce a single array that is name=>count. Any suggestions on a straightforward way to do this? I have looked at the Array Functions in the PHP Manual and didn't see anything that stood out as doing what I want, so I'm guessing I'll need a combinat...

How to convert ArrayList to an array of structure?

Here I have: Public Structure MyStruct Public Name as String Public Content as String End Structure Dim oStruct as MyStruct = New MyStruct() oStruct.Name = ... oStruct.Content = ... Dim alList as ArrayList = new ArrayList() alList.Add(oStruct) I'd like to convert the ArrayList to a static strongly-typed Array of type MyStruct....

Best practices for handling variable size arrays in c / c++?

If I have an array of a fixed size depending on how it is defined and used, I typically use one of two ways to reference it. Array type 1: Since it is a fixed size based on a define, I just use that define in all my loops referencing it. #define MAXPLAYERS 4 int playerscores[MAXPLAYERS]; for(i=0;i<MAXPLAYERS;++i) { .... do something ...

What's the best way to copy/fill a large array with a smaller array in C#?

I have a large int[] array and a much smaller int[] array. I want to fill up the large array with values from the small array, by repeat copying the small array into the large array until it is full (so that large[0] = large[13] = large[26] ... = small[0] etc.). I already have a simple method: int iSource = 0; for (int i = 0; i < dest...

Using arrays by reference in PHP

Hi, Why is the following code "crashing" in PHP?: $normal_array = array(); $array_of_arrayrefs = array( &$normal_array ); end( $array_of_arrayrefs )["one"] = 1; // choking on this one The expected result is that the final code line appends $normal_array with key "one" having value 1. In the real context of this scenario I use ...

How can I use array-references inside arrays in PHP?

I want to be able to do the following: $normal_array = array(); $array_of_arrayrefs = array( &$normal_array ); // Here I want to access the $normal_array reference **as a reference**, // but that doesn't work obviously. How to do it? end( $array_of_arrayrefs )["one"] = 1; // choking on this one print $normal_array["one"]; // sho...

How do I create a string from one row of a two dimensional rectangular character array in C#?

I have a 2 dimensional array, like so: char[,] str = new char[2,50]; Now, after I've stored contents in both str[0] and str[1], how do I store it in a string[] s = new string[2]; ? I tried s[0] = str[0].ToString(); but that seems to be an error: VC# expects 'two' indices within the braces, which means I can convert only a cha...

2D Javascript array

Simply put, is there a way to create a 2D javascript array using similar syntax to this? var newArray = [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ] ...

How to initialise a rather complex char array in C?

Assuming Visual C/C++ 6, I have a complex data structure of 22399 elements that looks like this: { { "(SAME", "AS", "U+4E18)", "HILLOCK", "OR", "MOUND"}, { "TO", "LICK;", {1, 1, 0}, "TASTE,", "A", "MAT,", "BAMBOO", "BARK"}, { "(J)", "NON-STANDARD", "FORM", "OF", "U+559C", ",", {1, 1, 0}, "LIKE,", "LOVE,", "ENJOY;", {1, 1, 4}, "JOYFUL", ...

How do you slice arrays in "D"?

How are arrays manipulated in "D"? ...

Convert dictionary values into array

what is the most efficient way of turning the list of values of a dictionary into an array for example, if i have: Dictionary where key is string and value is Foo i want to get Foo[] I am using VS 2005, C# 2.0 ...