arrays

Convert array of hashes to array of structs?

Let's say I have two objects: User and Race. class User attr_accessor :first_name attr_accessor :last_name end class Race attr_accessor :course attr_accessor :start_time attr_accessor :end_time end Now let's say I create an array of hashes like this: user_races = races.map{ |race| {:user => race.user, :race => race} } ...

Are variable length arrays possible with Javascript

I want to make a variable length array in Javascript. Is this possible. A quick google search for "Javascript variable length array" doesn't seem to yield anything, which would be surprising if it were possible to do this. Should I instead have a String that I keep appending to with a separator character instead, or is there a better w...

Writing/Reading struct w/ dynamic array through pipe in C

I have a struct with a dynamic array inside of it: struct mystruct{ int count; int *arr; }mystruct_t; and I want to pass this struct down a pipe in C and around a ring of processes. When I alter the value of count in each process, it is changed correctly. My problem is with the dynamic array. I am allocating the array as such: mys...

Is this the correct way to build a Perl hash that utilizes arrays?

This is the first time I have manipulated hashes and arrays in this way -- and it is working. Basically, for every key there are multiple values that I want to record and then print out in the form "key --> value --> value --> val..." My code is as follows. I am surprised that it works, so concerned that it works "by mistake". Is this t...

NSPredicate and arrays

Hello, I've got a short question. I have an NSArray filled with Cars (inherits from NSObject) Car has the @property NSString *engine (also regarded @synthesize ...) Now I want tu filter the array using NSPredicate predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(engine like %@)", searchText]]; newArray = [Arra...

Ruby - Subclassing array to make it randomize when flattened

I'm trying to subclass Array in ruby to make it randomize its elements when flatten! is called. Looking at the source code for Array#flatten (http://ruby-doc.org/core/classes/Array.src/M002218.html), it looks like it should recursively call flatten! on any array contained within an array. So, I tried doing something like this: class R...

C++ Arrays manipulations (python-like operations)

Hi Guys, I'm trying to figure out the best C++ library/package for array manipulations in a manner of python. Basically I need a simplicity like this: values = numpy.array(inp.data) idx1 = numpy.where(values > -2.14) idx2 = numpy.where(values < 2.0) res1 = (values[idx1] - diff1)/1000 res2 = (values[idx2] - diff2)*1000 In pyth...

Search string in file (C)

So my code isn't working... test.c:27: warning: passing argument 1 of ‘search’ from incompatible pointer type which is the fgets line. My code opens a file, reads the file line by line, and I'm trying to create a "search" function that will return a value that indicates whether that string is found on that line of the file. My ultim...

How do I perform this MutliArray setup in Java?

I come from a PHP background and I'm just getting my teeth into some Java. I was wondering how I could implement the following in Java as simply as possible, just echoing the results to a terminal via the usual "System.out.print()" method. <?php $Results[0]['title'] = "No Country for Old Men"; $Results[0]['run_time'] = "122 mins"; $Resu...

Duplicate array but maintain pointer links

Suppose I have an array of nodes (objects). I need to create a duplicate of this array that I can modify without affecting the source array. But changing the nodes will affect the source nodes. Basically maintaining pointers to the objects instead of duplicating their values. // node(x, y) $array[0] = new node(15, 10); $array[1] = ne...

Arranging MC that is called from XML backwards.

I'm loading my MC from XML backwards, and its separated by 10 per pages. When it's loaded to the stage it could not be arrange accordingly to grids. And when I click at other pages to let it run another 10 before the previous loaded XML object, it does not add in to the Array(I've clear off the array before reload the XML) private funct...

storing of strings in char arrays in C

#include<stdio.h> int main() { char a[5]="hello"; puts(a); //prints hello } Why does the code compile correctly? We need six places to store "hello", correct? ...

How to get array keys in Javascript?

I have an array created with this code: var widthRange = new Array(); widthRange[46] = { sel:46, min:0, max:52 }; widthRange[66] = { sel:66, min:52, max:70 }; widthRange[90] = { sel:90, min:70, max:94 }; I want to get each of the values 46, 66, 90 in a loop. I tried for (var key in widthRange) but this gives me a whole bunch of extra...

How to make all possible sum combinations from array elements in VB

If there is an array with elements: 1,2,3,4, the program should return another array with sum of all combinations: 1 2 3 4 3 (1+2) 4 (1+3) 5 (1+4) 5 (2+3) 6 (2+4) 7 (3+4) 6 (1+2+3) 7 (1+2+4) 8 (1+3+4) 9 (2+3+4) 10 (1+2+3+4) ...

Letting a new PHP page know a new POST value is an Array?

I have a page that creates an associative array, then passes it as a hidden value to a new PHP page. I have a foreach loop waiting to iterate through it but it coughs up an "invalid argument" error, as if the page doesn't know the value it's working with is an array (Despite "print" showing simply "Array"). This is essentially what I hav...

An array problem in C++

To access the array indice at the xth position we can use some sort of illustration as shown below #include<iostream> using namespace std; int main(){ float i[20]; for(int j=0;j<=20;j++) i[j]=0; } However the following piece of code does not work #include<iostream> using namespace std; float oldrand[55]; int jrand; ...

How can I sort a Perl array of array of hashes?

@aoaoh; $aoaoh[0][0]{21} = 31; $aoaoh[0][0]{22} = 31; $aoaoh[0][0]{23} = 17; for $k (0 .. $#aoaoh) { for $i(0.. $#aoaoh) { for $val (keys %{$aoaoh[$i][$k]}) { print "$val=$aoaoh[$i][$k]{$val}\n"; } } } The output is: 22=31 21=31 23=17 but i expect it to be 21=31 22=31 ...

How do I find the sum of all values from two different arrays in Perl?

How to find the sum of all values from two different arrays in Perl? @array1 = (1, 2, 3); @array2 = (10, 10, 10); @sumofarray1and2 = ? So I figured I can do two kinds of things here. I can do two foreach loops and add contents of @array1 and @array2 first then get the sum of both. ...

Using Mergesort to calculate number of inversions in C++

void MergeSort(int A[], int n, int B[], int C[]) { if(n > 1) { Copy(A,0,floor(n/2),B,0,floor(n/2)); Copy(A,floor(n/2),n-1,C,0,floor(n/2)-1); MergeSort(B,floor(n/2),B,C); MergeSort(C,floor(n/2),B,C); Merge(A,B,0,floor(n/2),C,0,floor(n/2)-1); } }; void Copy(int A[], int startIndexA, int endI...

Size of an Array

Hi all ! I'm new in objective C and iphone and it's hard from me. How do you get the size of an NSarray ? ? ? and print it in the console using NSLog ? thanks now it's basic but hard for me. ...