arrays

Perl: if ( element in list )

I'm looking for presence of an element in a list. In Python there is an in keyword and I would do something like: if element in list: doTask Is there something equivalent in Perl without having to manually iterate through the entire list? ...

How to add all items in a String array to a vector in Java ?

My code looks like this : Vector<String> My_Vector=new Vector<String>(); String My_Array[]=new String[100]; for (int i=0;i<100;i++) My_Array[i]="Item_"+i; ...... My_Vector.addAll(My_Array); But I got an error message, what's the right way to do it, without looping to add each item ? Frank ...

Pointer arithmetic and arrays: what's really legal?

Consider the following statements: int *pFarr, *pVarr; int farr[3] = {11,22,33}; int varr[3] = {7,8,9}; pFarr = &(farr[0]); pVarr = varr; At this stage, both pointers are pointing at the start of each respective array address. For *pFarr, we are presently looking at 11 and for *pVarr, 7. Equally, if I request the contents...

Comparing items based on their index in an array in Ruby

I have a Card class and I want to overload the > operator to compare against another card (Ace is higher than king, king higher than queen, etc). I've forgotten what little I ever knew about Ruby and don't have a clue where to start. class Card @@RANKS = ['A', 'K', 'Q', 'J', 'T', '9', '8','7','6','5','4','3','2'] attr_reader :rank ...

if (variable == [any item in a collection]) in Java

Let's say I have an array of primitives or a list of objects, doesn't matter, is there a short way of doing this kind of check: if (a_primitive == any_item_in_my_collection) { // do something } or if (an_object_ref.equals(any_item_in_my_collection)) { // do something } without doing this for (int i = 0; i < myArray.length;...

Looping through a JOIN'ed MySQL Associative Array

Right, so I've got a query that looks like this: $bestof_query = "SELECT * FROM physicians p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC "; $query = mysql_query($bestof_query); while($result = mysql_fetch_array($query)) { extract($result); ech...

PHP foreach over an array of objects

I'm trying to use a foreach loop for an array of objects. Inside of the BeginBattle() method I want to iterate through all of the objects and increment their played count automatically. Unfortunately, the web browser shows I have an error: Fatal error: Call to a member function BattleInitiated() on a non-object in /nfs/c05/h01/mnt/7029...

Checking if one array is the reverse of another array in java

Im trying to create a method that take 2 int array as the input parameter and returns true if the array are reverse and false otherwise. This is what I have so far but it is wrong. public static void main(String[] args) { int b,a; int[] data1 = {14,-70,-18,88,85,97,-65,13,-71,-12}; int[] data2 = {-12,-71,13,-65,97,85,88,-18,...

Select an array per row?

I have 2 tables: 1. products - product_id - title 2. product_categories - product_id - category_id Each product can have more than one category. I store this information in the product_categories table. I want to be able to SELECT all the category_ids when I select a product in 1 query. How can I do this? I have the following so far, ...

Finding placeholders and put then in an array

In my string I have place holders like: ##NEWSLETTER## , ##FOOTER# ##GOOGLEANALYTICS## etc. Each of those placeholders is delimited by: ## I want to find each of thos placeholders and put them in an array. The tricky part is that what's inside the ## delimiters can be anything. ...

Select a random item from an array

I'm creating a bot in Shell Script: # Array with expressions expressions=("Ploink Poink" "I Need Oil" "Some Bytes are Missing!" "Poink Poink" "Piiiip Beeeep!!" "Hello" "Whoops! I'm out of memmory!") # Seed random generator RANDOM=$$$(date +%s) # Loop loop loop loop loop loop ... while [ 1 ] do # Get random expression... select...

Read a file's content (with spaces) into an array

I'm creating a Shell Script, and I have a file like this called expressions.txt: "Ploink Poink" "I Need Oil" "Some Bytes are Missing!" "Poink Poink" "Piiiip Beeeep!!" "Whoops! I'm out of memory!" "1 + 1 = 3" "Please fix my bugs!" "Goeiedag!" "Hallo!" "Guten Tag!" "Hyvää Päivää!" "Добрый день" "!สวัสดี" "Bonjour!" "!مرحبا" "!שלום" "Γειά!...

What is the MPI code to extract a 2D sub-matrix from a larger 2D matrix?

Hello all. I am looking for the best way of extracting a 2D sub-matrix from a larger 2-D submatrix. That is. If I have a matrix with 1 ghost point on each edge, I want to extract the interior matrix. So if the matrix is defined as matrix[NX+2][NY+2] how do I extract out the submatrix starting at matrix[1][1] going to matrix[NX+1][NY+1] ...

Checking element values in a Boolean array - C#

I'm writing some error checking and trying to make use of an boolean array to store true or false in the elements and then my final condition parses through the stored elements to determine if its all true in visual studio 2008. Theres probably a easier way to do the error checking, but might as well learn how to utilize an array. Here's...

Bash, argument list segment

If you have a list in python, and you want the elements from 2 to n can do something nice like list[2:] I'd like to something similar with argv in Bash. I want to pass all the elements from $2 to argc to a command. I currently have command $2 $3 $4 $5 $6 $7 $8 $9 but this is less than elegant. Would would be the "proper" way ...

Check an array of session values in PHP

There is an array of user states stored in the session. This works: <?php if ($_SESSION['_app_user']['data']['state']['1']) { ?> <p>User has state 1</p> <?php } ?> But, selecting multiple states doesn't: <?php if ($_SESSION['_app_user']['data']['state']['1,6,10']) { ?> <p>User has state 1 or 6 or 10</p> <?php } ?> How can y...

Redimming arrays in VBA

I have 3 arrays of data, that are filled by reading off of an excel sheet, some of the points of data are missing and as such have just been entered into excel as "NA" so I want to look through my array and find each instance of these NA's and remove them from the array since the information is useless. I need to update all three arrays ...

Why is it preferred to use Lists instead of Arrays in Java?

Many people and authors suggested to us to use list than array. List <Integer> list = new ArrayList<Integer>(); list.addElement(1); .... What it is the reason behind it? ...

How many elements of array are not null ?

An array is defined of assumed elements like I have array like String[] strArray = new String[50];. Now from 50 elements only some elements are assigned and remaining are left null then I want the number of assigned elements. Like here only 30 elements are assigned then I want that figure. ...

Find value assoaction in array

Hello, i have a multi dimension array with sub array having repeated values of 'eduHisRowId' like: Array ( [0] => Array ( [eduHisRowId] => 4 [repOrderId] => 15 ) [1] => Array ( [eduHisRowId] => 5 [repOrderId] => 16 ) [2] => Array ( ...