arrays

Why does my Perl max() function always return the first element of the array?

I am relatively new to Perl and I do not want to use the List::Util max function to find the maximum value of a given array. When I test the code below, it just returns the first value of the array, not the maximum. sub max { my @array = shift; my $cur = $array[0]; foreach $i (@array) { if($i > $cur) { ...

Iterating through a variable length Java array

How do I iterate over a Java array of variable length. I guess I would setup a while loop, but how would I detect that I have reached the end of the array. I guess I want something like this [just need to figure out how to represent myArray.notEndofArray()] index = 0; while(myArray.notEndofArray()){ system.out.println(myArray(index...

Confusing with memory allocation of array in Java

I get annoying to comprehend memory allocation of array in Java,could your guys give me some clarification about this.Following is an example that i want you to give me the result of how many object get instantiated for each statement. String s1[] = {"12","saf"}; int s2[] = {1,2}; Object s3[] = new Object[2]; String s4[][] = {{"12","...

Finding if a target number is the sum of two numbers in an array via LINQ

A basic solution would look like this: bool sortTest(int[] numbers, int target) { Array.Sort(numbers); for(int i = 0; i < numbers.Length; i++) { for(int j = numbers.Length-1; j > i; j--) { if(numbers[i] + numbers[j] == target) return true; } } return false; } Now I'm v...

Why does Perl treat a hash element as list context at declaration?

Given this code: #!/usr/bin/perl -w use strict; use warnings; sub foo { return wantarray ? () : "value1"; } my $hash = { key1 => foo(), key2 => 'value2' }; use Data::Dumper; print Dumper($hash); I get the following output: $VAR1 = { 'key1' => 'key2', 'value2' => undef }; When I would expect: $VAR1 = { 'key1' ...

Return common element indices between two numpy arrays

Hi all, I have two arrays, a1 and a2. Assume len(a2) >> len(a1), and that a1 is a subset of a2. I would like a quick way to return the a2 indices of all elements in a1. The time-intensive way to do this is obviously: from operator import indexOf indices = [] for i in a1: indices.append(indexOf(a2,i)) This of course takes a long...

create array of object help

I'm having the following classes. While trying to set the values of the employee class with the following code, i`m getting the error message:Object reference not set to an instance of an object. How can I solve it? public class Employee { public Test[] test{ get; set; } public Employee() { this.test[0]...

Return a float array in C++

Hi, I currently have a 4x4 matrix class in C++ and I store each value as a float: Matrix4d::Matrix4d(const float& m00, const float& m01, const float& m02, const float& m03, const float& m10, const float& m11, const float& m12, const float& m13, const float& m20, const float& m21, const float& m22, ...

Arbitrary-sized string arrays in C#

I want to define an array of strings but I don't know the number of strings I'll need to store in it. Can I define such an array, and how can I insert strings into it? ...

Override BigDecimal to_s default in Ruby

As I retrieve data from a database table an array is populated. Some of the fields are defined as decimal & money fields and within the array they are represented as BigDecimal. I use these array values to populate a CSV file, but the problem is that all BigDecimal values are by default represented in Scientific format (which is the de...

How can I create an empty array in objective C, and assign value into it one by one?

In Java, I can do that : int[] abc = new int[10]; for(int i=0; i<abc.length; i++){ abc[i] = i; } How can I implement similar thing in Objective C? I see some answer using NSMutableArray, wt's different between this and NSArray? ...

Why check if (*argv == NULL)?

In the data structures class that I am currently taking, we have been tasked with writing a web crawler in C++. To give us a head start, the professor provided us with a program to get the source from a given URL and a simple HTML parser to strip the tags out. The main function for this program accepts arguments and so uses argc/argv. Th...

Java Object Question

Not quite sure how to word this question. I am wondering if there is a method to check certain parts of a custom java class to see if it matches a certain criteria. Such as this public Name(String forename, String middlename, String surname) And then when an array of instances of that class are created say, Name[] applicants = new N...

CakePHP 1.2.6 / PHP5.2.12 Error in Array Loop in Assignment by Reference

I'm working on retrieving a stack of data and for some reason some of the data gets corrupted. For instance, I've got some Post models that each are related to Comment models (hasMany), and each of the Comment models belongsTo a User. When retrieving the data, here's what I get from the database for the comments: [Post] => Array ( ) [...

How do I do multiple assignment in MATLAB?

Here's an example of what I'm looking for: >> foo = [88, 12]; >> [x, y] = foo; I'd expect something like this afterwards: >> x x = 88 >> y y = 12 But instead I get errors like: ??? Too many output arguments. I thought deal() might do it, but it seems to only work on cells. >> [x, y] = deal(foo{:}); ??? Cell content...

How can I pass in an array as a value into a PHP soapclient request?

How can I pass in an array as a value into a PHP soapclient request? I have a soapclient instantiated and connected already. I then try to make a call to a webservice method that expects 3 parameters (string, string, hashmap). Here is what I expected to work below. But when viewing the xml output, the params node is empty. soapclient-...

What is the maximum length of an array in .NET on 64-bit Windows

I heard from someone that the maximum array size in .NET is 4 GB? Just wondering if that is true. You wouldn't dream of doing this on 32-bit .NET but on a 64-bit system with 12 GB of RAM, maybe, just maybe you might want to do this. :-) ...

How to invoke C compiler under gcc

According to my memory the following piece of code should compile fine on C++ but not in C. Only problem is how to test it? It compiled fine with g++ and also with gcc. I'm assuming that g++ is C++ compiler and gcc is C compiler. I've tried it with mingw under Windows. Am I correct? if not then how to compile it using C compiler. int ma...

SQLite delete from table where field does not match an object in an array

Say I have a table with an ID field, and I have a local array (in any language, php or objective c or basic or whatever) of eligible IDs. I want to delete any record in which the ID field does not match any of the eligible IDs in my array. Is there any way to do this in a single SQL query? Can you pass an array into SQL and basically sa...

String to Arrays conversion

When this is valid: string s4 = "H e l l o"; string[] arr = s4.Split(new char[] { ' ' }); foreach (string c in arr) { System.Console.Write(c); } Why This is invalid string s4 = "H e l l o"; char[] arr = s4.Split(new char[] { ' ' }); foreach (char c in arr) { System.Console.Write(c); } Cant We build a C...