Getting different output from manual vs. programmatic arrays

bash array
bash array of strings
bash associative array
powershell array foreach
arrays java
bash array length
append to array bash
java array methods

I’m getting some weird results implementing cyclic permutation on the children of a multidimensional array.

When I manually define the array e.g.

arr = [
  [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]

the output is different from when I obtain that same array by calling a method that builds it.

I’ve compared the manual array to the generated version and they’re exactly the same (class and values, etc).

I tried writing the same algorithm in JS and encountered the same issue.

Any idea what might be going on?

def Build_array(child_arr, n)
    #Creates larger array with arr as element, n times over. For example Build_array([1,2,3], 3) returns [[1,2,3], [1,2,3], [1,2,3]] 

    parent_arr =

    0.upto(n) do |i|
        parent_arr[i] = child_arr

    return parent_arr

def Cylce_child(arr, steps_tocycle)    
    # example: Cylce_child([1, 2, 3, 4, 5], 2) returns [4, 5, 1, 2, 3]

    0.upto(steps_tocycle - 1) do |i|
        x = arr.pop()

    return arr

def Permute_array(parent_array, x, y, z)
    #x, y, z = number of steps to cycle each child array

    parent_array[0] = Cylce_child(parent_array[0], x)
    parent_array[1] = Cylce_child(parent_array[1], y)
    parent_array[2] = Cylce_child(parent_array[2], z)

    return parent_array

arr = Build_array([1, 2, 3, 4, 5], 4)
# arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]

puts "#{Permute_array(arr, 1, 2, 3)}"

# Line 34: When arr = Build_array([1, 2, 3, 4, 5], 4) 
# Result (WRONG):
#  [[5, 1, 2, 3, 4], [5, 1, 2, 3, 4], [5, 1, 2, 3, 4], [5, 1, 2, 3, 4]]
# Line 5: When arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, # 2, 3, 4, 5]]
# Result (CORRECT):
#   [[5, 1, 2, 3, 4], [4, 5, 1, 2, 3], [3, 4, 5, 1, 2], [1, 2, 3, 4, 5]]

The problem is in the way you build the array.

This line:

parent_arr[i] = child_arr

does not put in parent_arr[i] a copy of child_arr but a reference to it.

This means your initial array contains four references to the same child array. Later on, when the code changes parent_arr[0], it changes the same array that child_arr was referring to in the build method. And that array is also parent_arr[1] and parrent_arr[2] and so on.

A simple solution to the problem is to put in parent_arr[i] a copy of child_arr:

parent_arr[i] =

Arrays, sets, lists, and records, Unlike sets, a list may contain the same element several times. Arrays. In the first part of this manual we have already encountered many examples that make use of arrays. The output produced by this model (file arraydef.mos) is the following. It is also possible to delete some or all cells of a dynamic array using the� A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null. Arrays are zero indexed: an array with n elements is indexed from 0 to n-1. Array elements can be of any type, including an array type. Array types are reference types derived from the abstract base type Array.

I see where the bug was. Added the clone method to line 8 so that it now reads:

parent_arr[i] = child_arr.clone

#Old: parent_arr[i] = child_arr

Thanks Robin, for pointing me in the right direction.

Introduction to Bash arrays, We would like to capture that output at each iteration and save it in another array so we can do various manipulations with it at the end. Java supports arrays, but there is a little difference in the way they are created in Java using the new operator. You can try to execute the following program to see the output, which must be identical to the result generated by the above C example.

This is a fairly common mistake to make in Ruby since arrays do not contain objects per-se, but object references, which are effectively pointers to a dynamically allocated object, not the object itself.

That means this code:, [ ])

Will yield an array containing four identical references to the same object, that object being the second argument.

To see what happens:, [ ]).map(&:object_id)
# => => [70127689565700, 70127689565700, 70127689565700, 70127689565700]

Notice four identical object IDs. All the more obvious if you call uniq on that.

To fix this you must supply a block that yields a different object each time: { [ ] }.map(&:object_id)
# => => [70127689538260, 70127689538240, 70127689538220, 70127689538200]

Now adding to one element does not impact the others.

That being said, there's a lot of issues in your code that can be resolved by employing Ruby as it was intended (e.g. more "idiomatic" code):

def build_array(child_arr, n)
  # Duplicate the object given each time to avoid referencing the same thing
  # N times. Each `dup` object is independent. do

def cycle_child(arr, steps_tocycle)
  # Ruby has a rotate method built-in

# Using varargs (*args) you can just loop over how many positions were given dynamically
def permute_array(parent_array, *args)
  # Zip is great for working with two arrays in parallel, they get "zippered" together.
  # Also map is what you use for transforming one array into another in a 1:1 mapping do |a, p|
    # Rotate each element the right number of positions
    cycle_child(p, -a)

arr = build_array([1, 2, 3, 4, 5], 4)
# => [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]

puts "#{permute_array(arr, 1, 2, 3)}"
# => [[5, 1, 2, 3, 4], [4, 5, 1, 2, 3], [3, 4, 5, 1, 2]]

A lot of these methods boil down to some very simple Ruby so they're not especially useful now, but this adapts the code as directly as possible for educational purposes.

Arrays (The Java™ Tutorials > Learning the Java Language , Arrays. An array is a container object that holds a fixed number of values of a single type. puts some values in the array, and prints each value to standard output. In a real-world programming situation, you would probably use one of the Like declarations for variables of other types, an array declaration has two� Plot the numeric arrays. Return an array of chart line objects from the plot function and use them to add different markers to each set of data points. arrayfun can return arrays of any data type so long as objects of that data type can be concatenated.

PowerShell Array Guide: How to Use and Create, Arrays make it possible to ingest, manipulate and output true data structures Then we'll look at several different types of arrays, and explore some of the Get the Free PowerShell and Active Directory Essentials Video Course An item can be a string, an integer, an object, or even another array, and� What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

Arrays \, The term array refers to a structured grouping or an imposing number: “The dinner In computer programming, an array is a set of data elements stored under the same name. There are different ways to declare, create, and assign arrays. Option A) 0 0 B)[I@6bc7c054 0 C) 0 0 0 0 0 0 D) none. Output: B Explanation : arr : It is giving the base address of array arr[0] : It is giving value of array element at 0 th location.

Arrays in C Programming with Examples, Arrays in C allow you to store multiple items of the same data type, such as a list of integers. Arrays You can use the indexes to set or get specific values from the array. declare input and output arrays, and variables With the initializer list/string syntax, you can also set the array size manually if you wish to, but this isn't� Introduction to 2D Arrays In Python. Arrangement of elements that consists of making an array i.e. an array of arrays within an array. A type of array in which the position of a data element is referred by two indices as against just one, and the entire representation of the elements looks like a table with data being arranged as rows and columns, and it can be effectively used for performing

  • I don't know Ruby, but certainly in JS, and in Python too, this is expected because all elements in the output of build_array are different references to the same array, so any change made to one, such as all those inside Cycle_child, affects all of them. While I don't know for sure, I expect this is what is happening in Ruby too.
  • Note: Ruby is a case-sensitive language and capital letters have specific meaning in terms of syntax. Variables and method names should be lower-case letters. Capitals indicate constants of the form ClassName or CONSTANT_NAME.
  • Thanks Robin, that was the issue. (@tadman: thanks for the heads-up)