arrays

RoR show elements in two columns

how can i split elements of a model into two equally sized pieces so that i can show them in two columns? i have this: element 1 element 2 element 3 element 4 element 5 and i want this: element 1 element 4 element 2 element 5 element 3 split() unfortunately removes the middle element. ...

Json.NET: serializing/deserializing arrays

I'm using a Json.NET library. What's the most convenient way of serializing/deserializing arrays in JSON via C#? For example, I'm trying to deserialize the following text (reading from file): { "Name": "Christina", "Gender": "female", "Favorite_numbers": [11, 25 ,23] } I'm reading the text above from file to a variable: J...

Reading text file into an array of lines in C

Using C I would like to read in the contents of a text file in such a way as to have when all is said and done an array of strings with the nth string representing the nth line of the text file. The lines of the file can be arbitrarily long. What's an elegant way of accomplishing this? I know of some neat tricks to read a text file dire...

In JavaScript, why is [ ] preferred over new Array(); ?

I remember reading somewhere (I think it was in one of Crockford's papers) that using an array literal [] is better than using the new Array(); notation. But I can't really remember any advantages of one over the other. Can anyone please explain to me on why the former is preferred over the latter? [Update] Here is a reason on why []...

RoR / Ruby delete nil elements from nested array

in a previous question i asked how to split an array into two equal pieces in ruby on rails. this is how i did it: >> a = [1,2,3,4,5] => [1, 2, 3, 4, 5] >> a.in_groups_of( (a.size/2.0).ceil ) if a.size > 0 => [[1, 2, 3], [4, 5, nil]] now i've got a nested array that contains nil elements if the size of the array is odd. how can i remo...

Comparing character arrays with an == operator in C

I know that the correct way to compare "strings" in C is by using strcmp, but now I tried comparing some character arrays with the == operator, and got some strange results. Take a look at the following code: int main() { char *s1 = "Andreas"; char *s2 = "Andreas"; char s3[] = "Andreas"; char s4[] = "Andreas"; cha...

Array of Dates in Java

I know how to create an array of strings or integers, but how does one create an array of dates :/ ...

Use a Plist dictionary for App settings

I want to be able to use a plist for settings Im implementing in my app. I want a dictionary "Settings" to hold my arrays, such as "Debug", "Option 1", Option 2", etc. How would I access "Debug"array under the "Settings" dictionary? This is what my code looks like: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirect...

C - Dynamically initializing arrays.

I'm trying to find the equivalent of int myArray[n], except I don't know what n is without input. Is the following code supposed to work? (I need to use kmalloc instead of malloc). int * pages; //... later, after we find out n... pages = (int *)kmalloc(npages * sizeof(int)); Debugging using gdb, the value pages[0] doesn't seem to be ...

How do you add an array to another array in Ruby and not end up with a multi-dimensional result?

somearray = ["some", "thing"] anotherarray = ["another", "thing"] somearray.push(anotherarray.flatten!) I expected ["some","thing","another","thing"] ...

Problem filtering an Array into multiple Tableviews (based on SeismicXML)

I've taken the Seismc XMl example and adjusted it to my needs, I've added a tab bar controller with multiple tabs each with a TableView, so far so good, I now want to filter the main TableView, so that different info appears in each of the TableViews based on category. I've created a class for each filter "type" in the app delegate, and...

Can you say that associative arrays in PHP are like 2D arrays?

Can you say that associative arrays in PHP are like 2D arrays? ...

jQuery retrieving array from attribute value

If I have: <a id="anchor" href="some link" rel="{ key1: 'value', key2 : 'value'}">text</a> How do retrieve the values in the 'rel' attribute so i can refer to them using dot notation? eg. $('#anchor').attr('rel').key1 ...

Array of in_addr

Hi, I would like to create a array of in_addr using gethostbyname(). After looking on Google, I have found this short code (at http://www.logix.cz/michal/devel/various/gethostbyname.c.xp): /* * gethostbyname.c - Example of using gethostbyname(3) * Martin Vidner <[email protected]> */ #include <stdio.h> #include <netdb.h> #include <n...

C# Extend array type to overload operators

I'd like to create my own class extending array of ints. Is that possible? What I need is array of ints that can be added by "+" operator to another array (each element added to each), and compared by "==", so it could (hopefully) be used as a key in dictionary. The thing is I don't want to implement whole IList interface to my new clas...

iPhone Development xcode : Putting user input (names) into text array

I am trying to load names in an array using UIAlert and then print them one by one. I can not figure this out can anyone help me with this. I would really your help apprecaite ...

php Array Initialization

Hi, I need to initialize an array of objects in PHP. Presently I have the following code: $comment = array(); And when i am adding an element to the array public function addComment($c){ array_push($this->comment,$c); } Here, $c is an object of class Comment. But when I try to access an functions of that class using $comment,...

Quicksort to order array by business key?

Hi, I've an array of objects, the size of which I cannot predict. The contents of the array are model objects with properties of type nsstring and nsnumber. I need to sort the array according to one of the properties, an nsnumber. How would you do it in objective-c/Cocoa? Implement quicksort or some other algorithm (which one)? Any lib...

Java: Finding the highest value in an array

For some reason this code is printing three values for the highest value in the array when I'm trying to print just one (which is 11.3). Can someone please explain to me why it is doing this? Thanks. import java.util.Scanner; public class Slide24 { public static void main (String [] args) { Scanner in = new Scanner(Sys...

Initialize a list of objects in Python

Hi all, I'm a looking to initialize an array/list of objects that are not empty -- the class constructor generates data. In C++ and Java I would do something like this: Object lst = new Object[100]; I've dug around, but is there a Pythonic way to get this done? This doesn't work like I thought it would(I have 100 references to the s...