arrays

Create and populate two-dimensional array in Scala

What's the recommended way of creating a pre-populated two-dimensional array in Scala? I've got the following code: val map = for { x <- (1 to size).toList } yield for { y <- (1 to size).toList } yield (x, y) How do I make an array instead of list? Replacing .toList with .toArray doesn't compile. And is there a more co...

How do I randomly fill an array in Java?

I'm writing a program that creates a 2D array from a integer n. I then have to fill the array with values from 1 to the n*n array size and check to see if it is a magic square. The way I am doing it now fills the array in order from 1 to n*n array size. How can I make that random? My code: System.out.print("Enter an whole number: "); ...

accessing array values without square brackets in php

In php how can I access an array's values without using square brackets around the key? My particular problem is that I want to access the elements of an array returned by a function. Say function(args) returns an array. Why is $var = function(args)[0]; yelling at me about the square brackets? Can I do something like $var = funct...

search array and get array key

Hi all, I have an array, which I'd like to search for a value in and retreive the array key if it exists, but not sure how to even go about doing that. Here's my array: Array ( [hours] => Array ( [0] => 5 [1] => 5 [2] => 6 [3] => 6 [4] => 8 [5] => 10 ...

Find key of parent in array / PHP

Perhaps someone can help me out with this one: I'm using a basic search function to find an array deep within an array. The problem is, once that array is found, I'd also like to return it's parent key. Is there a PHP function that can determine the parent key of an array? Below is an example of the Search Function... Ideally I'd like...

in Python find number of same elements in 2 lists

Hi, In Python if I have 2 lists say: l1 = ['a', 'b', 'c', 'd'] l2 = ['c', 'd', 'e'] is there a way to find out how many elements they have the same. In the case about it would be 2 (c and d) I know I could just do a nested loop but is there not a built in function like in php with the array_intersect function Thanks ...

Iterating over two dimension array and knowing current position

I am trying to iterate a multidimension array created with the following line To iterate i'm using the following code visiblematrix= Array.new (10) {Array.new(10){0}} But this doesn't allow me to know the current x,y position while iterating. how can i find it out without resorting to temporary variables visiblematrix.each do |x| ...

Number of different elements in an array.

Is it possible to compute the number of different elements in an array in linear time and constant space? Let us say it's an array of long integers, and you can not allocate an array of length sizeof(long). P.S. Not homework, just curious. I've got a book that sort of implies that it is possible. ...

Multi-Dimensional Array and ArrayIndexOutOfBoundsException

Hello, I have a strange problem which I can't fix: A field: private boolean[][][] gaps; Constructor (1st line): gaps = new boolean[NOBARRICADES][WIDTH][HEIGHT]; Constructor (2nd line): for (int i = 0; i < NOBARRICADES; i++) { Java throws an error for the 2nd line, saying: Exception in thread "main" java.lang.ArrayIndexOutO...

How to get values of an array in the alphabetical order?

I have an array of strings plus one additional string. I want to use this string and values of array to get a set of string. Then I want to order the set of string alphabetically and extract the string which is the first in the list. What is the easiest way to do it in Java? ADDED: I wanted to do it this way: List<String> playersList ...

Javascript array of dates - not iterating properly (jquery ui datepicker)

Hi I have some code which builds an array of date ranges. I then call a function (from the jquery UI datepicker), passing it a date, and compare that date with dates in the array. I'm doing it this way because the dates are stored in a cms and this is the only way I can output them. Unfortunately my code only checks the first date ran...

How to insert an Array/Objet into SQL (bestpractice)

I need to store three items as an array in a single column and be able to quickly/easily modify that data in later functions. [---YOU CAN SKIP THIS PART IF YOU TRUST ME--] To be clear, I love and use x_ref tables all the time but an x_ref doesn't work here because this is not a one-to-many relationship. I am making a project managem...

How to create a array of boost matrices?

How can I define a array of boost matrices as a member variable? None of the following worked. boost::numeric::ublas::matrix<double> arrayM(1, 3)[arraySize]; boost::numeric::ublas::matrix<double>(1, 3) arrayM[arraySize]; boost::numeric::ublas::matrix<double> arrayM[arraySize](1, 3); Thanks, Ravi. ...

passing an array of structures (containing two mpz_t numbers) to a function

Hello, I'm working on some project where I use the type mpz_t from the GMP C library. I have some problems passing an array of structures (containing mpz_ts) adress to a function : I wille try to explain my problem with some code. So here is the structure : struct mpz_t2{ mpz_t a; mpz_t b; }; typedef struct mpz_t2 *mpz_t2;...

In C, how do I check if a String contains 2 digits, 1 letter and 4 digits?

In the method I've tried this: int 1, 2, 4, 5, 6, 7; char 3; char display[10]; scanf("%d%d%c%d%d%d%d", &1, &2, &3, &4, &5, &6, &7); display = {1, 2, 3, 4, 5, 6, 7}; But I get errors everywhere and it doesn't work. ...

foreach loop corrupting my array?

Explanation I have a multidimensional array that is iterated over to created a categorized view of people with different research interests. The main array look something like this: Array ( ... ['Cell Biology'] => Array(4 elements) ['Molecular'] => Array(6 elements) ['Biology Education'] => Array(14 element...

Multi-dimensional array in php

Hi all, I would like to create a multi-dimensional array with two variables but don't know how. This is what i have so far; $_SESSION['name'][] = $row_subject['name']; $_SESSION['history'][]= $_SERVER['REQUEST_URI']; I wanted to know if this is possible? $_SESSION['name'][] = $row_subject['name'],$_SERVER['REQUEST_URI']; i want...

PHP Populating array with $variables

Hi, I'm trying to create a basic shopping cart, having an issue with the product page allowing users to add more items to their cart then are in stock (I have code in place to prevent this on the view cart page, just not the view product page) This is what I have so far; for ($i = 0; $i < $numItem; $i++) { extract($cartContent[$i]); $s...

PHP - Find parent key of array

I'm trying to find a way to return the value of an array's parent key. For example, from the array below I'd like to find out the parent's key where $array['id'] == "0002". The parent key is obvious because it's defined here (it would be 'products'), but normally it'd be dynamic, hence the problem. The 'id' and value of 'id' is known th...

Sort by the mode in an Array in Objective-C iPhone

If I have an NSArray with some values in them. Is there a way using descriptors to sort it by the most frequent number in the array first and the least frequent number at the end, Array has( "3", "2", "1", "3", "3", "7", ) to Array has( "3", "3", "3", "1", "2", "7", ) ...