How to output the value when there is an array in the array

Ordinary array

colors =["white", "black", "red", "green", "blue"]

There are 5 elements in an array called colors. Index numbers are assigned in order from the beginning, such as 0, 1, 2, 3 ...

When outputting

puts colors[2]

Then you can retrieve the third * red *.

Array in array

animals = [["tuna", "octopus", "shark"], ["dog", "cat", "pig"], ["crow", "swan", "eagle"]]

It's like this. In the array called animals, there is an array containing 3 types of sea animals, 3 types of land animals, and 3 types of sky animals.

Output all the contents at once

puts animals

<Output result>
tuna
octopus
shark
dog
cat
pig
crow
swan
eagle

Output only sea animals

Sea animals are assigned an index number of 0.

puts animals[0]

<Output result>
tuna
octopus
shark

Output only octopus

Among the sea animals with index number 0 In addition, index number 1 is assigned.

puts animals[0][1]

<Output result>
octopus

All output by specifying

=> Represents what is output.

puts animals[0][0]
=> tuna
puts animals[0][1]
=> octopus
puts animals[0][2]
=> shark
puts animals[1][0]
=> dog
puts animals[1][1]
=> cat
puts animals[1][2]
=> pig
puts animals[2][0]
=> crow
puts animals[2][1]
=> swan
puts animals[2][2]
=> eagle

More complex arrays

Even if the structure becomes complicated, it can be taken out in the same way.

countries = [["Japan", "America"], [["Brazil", "Russia"],["China", "India"]]]

There are two arrays in the array called countries Inside the second array is yet another array.

puts countries[0]

<Output result>
Japan
America
puts countries[1]

<Output result>
Brazil
Russia
China
India
puts countries[1][0]

<Output result>
Brazil
Russia

All output by specifying

puts countries[0][0]
=> Japan
puts countries[0][1]
=> America
puts countries[1][0][0]
=> Brazil
puts countries[1][0][1]
=> Russia
puts countries[1][1][0]
=> China
puts countries[1][1][1]
=> India

Even if it becomes more quadruple from here, the value can be retrieved with the same description.

Recommended Posts

How to output the value when there is an array in the array
How to retrieve the hash value in an array in Ruby
Null value is entered when assigning to an array
If the parameter is an array, how to include it in Stopara's params.permit
When there is no output to stdout in docker log
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
How to write the view when Vue is introduced in Rails?
How to specify an array in the return value / argument of the method in the CORBA IDL file
How to solve the problem when the value is not sent when the form is disabled in rails and sent
[Ruby] How to prevent errors when nil is included in the operation
[Java] How to search for a value in an array (or list) with the contains method
Throw an exception and catch when there is no handler corresponding to the path in spring
[Swift] How to get the number of elements in an array (super basic)
How to output standard from an array with forEach
How to set when "The constructor Empty () is not visible" occurs in junit
[Rails] How to display an image in the view
How to set chrony when the time shifts in CentOS7
How to change a string in an array to a number in Ruby
How to add the same Indexes in a nested array
[Ruby] How to batch convert strings in an array to numbers
How to perform a specific process when the back button is pressed in Android Fragment
[Ruby] How to count even or odd numbers in an array
[Swift5] How to get an array and the complement of arrays
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to solve the unknown error when using slf4j in Java
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
Monkey patch to return the current time when an empty string is specified in Ruby's Time.parse
I want to make the frame of the text box red when there is an input error
How to pass the value to another screen
How to get the date in java
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
How to output the sum of any three numbers, excluding the same value
What to do and how to install when an error occurs in DXRuby 1.4.7
How to convert an array of Strings to an array of objects with the Stream API
Get the type of an array element to determine if it is an array
[Docker] How to build when the source code is bind-mounted on the container
[Java small story] Monitor when a value is added to the List
Initial value when there is no form object property in Spring request
Is there an engineer who hasn't validated the Json column in Rails?
How to find the total number of pages when paging in Java
How to get the value after "_" in Windows batch like Java -version
How to constrain the action of the transition destination when not logged in
How to get the setting value (property value) from the database in Spring Framework
How to change the value of a variable at a breakpoint in intelliJ
I want to display an error message when registering in the database
[Swift] How to set an image in the background without using UIImageView.
Android development, how to check null in the value of JSON object
How to reference a column when overriding the column name method in ActiveRecord
How to create your own annotation in Java and get the value
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
[JPA] How to save when there are items other than keys in the intersection table related to ManyToMany
(Memo) How to solve dummy output in Ubuntu 20.04
How to check the logs in the Docker container
How to color code console output in Eclipse
When the server fails to start in Eclipse
How to use an array for HashMap keys
How to add sound in the app (swift)
How to delete the database when recreating the application
Output of how to use the slice method
How to solve an Expression Problem in Java