ruby 数组删除部分数组_在Ruby中创建数组的基本指南

喜欢ヅ旅行 2022-12-06 15:40 204阅读 0赞

ruby 数组删除部分数组

Storing variables within variables is a common thing in Ruby and is often referred to as a “data structure.” There are many varieties of data structures, the most simple of which is the array.

在变量中存储变量是Ruby中常见的事情,通常被称为“ 数据结构” 。 数据结构有很多种,其中最简单的就是数组。

Programs often have to manage collections of variables. For example, a program that manages your calendar must have a list of the days of the week. Each day must be stored in a variable, and a list of them can be stored together in an array variable. Through that one array variable, you can access each of the days.

程序通常必须管理变量的集合。 例如,管理您的日历的程序必须具有星期几的列表。 每天必须存储在一个变量中,并且它们的列表可以一起存储在数组变量中。 通过该数组变量,您可以访问每一天。

创建空数组 ( Creating Empty Arrays )

You can create an empty array by creating a new Array object and storing it in a variable. This array will be empty; you must fill it with other variables to use it. This is a common way to create variables if you were to read a list of things from the keyboard or from a file.

您可以通过创建一个新的Array对象并将其存储在变量中来创建一个空数组。 该数组将为空; 您必须使用其他变量填充它才能使用它。 如果要从键盘或文件中读取内容列表,这是创建变量的常用方法。

In the following example program, an empty array is created using the array command and the assignment operator. Three strings (ordered sequences of characters) are read from the keyboard and “pushed,” or added to the end, of the array.

在下面的示例程序中,使用array命令和赋值运算符创建一个空数组。 从键盘上读取三个字符串(字符的有序序列),然后“推”或将其添加到数组的末尾。

array = Array.new
数组= Array.new
3.times do
3.次做
str = gets.chomp
str = gets.chomp
array.push str
array.push str
end
结束

使用数组文字存储已知信息 ( Use an Array Literal to Store Known Information )

Another use of arrays is to store a list of things you already know when you write the program, such as the days of the week. To store the days of the week in an array, you could create an empty array and append them one by one to the array as in the previous example, but there is an easier way. You can use an array literal.

数组的另一种用法是存储您在编写程序时已经知道的事情的列表,例如一周中的几天。 要将星期几存储在数组中,您可以 创建一个空数组,并将它们一个一个地追加到数组中,如前面的示例所示,但是有一种更简单的方法。 您可以使用数组文字

In programming, a “literal” is a type of variable that’s built into the language itself and has a special syntax to create it. For example, 3 is a numeric literal and “Ruby” is a string literal. An array literal is a list of variables enclosed in square brackets and separated by commas, like [ 1, 2, 3 ]. Note that any type of variables can be stored in an array, including variables of different types in the same array.

在编程中,“文字”是一种内置于语言本身的变量,并具有创建它的特殊语法。 例如, 3是数字文字,而“ Ruby”是字符串文字 。 数组文字是由方括号括起来并用逗号分隔的变量的列表,例如[1,2,3] 。 请注意,任何类型的变量都可以存储在数组中,包括同一数组中不同类型的变量。

The following example program creates an array containing the days of the week and prints them out. An array literal is used, and the each loop is used to print them. Note that each is not built into the Ruby language, rather it’s a function of the array variable.

下面的示例程序创建一个包含星期几的数组并将其打印出来。 使用数组文字,并使用each循环打印它们。 请注意, 它们并不是Ruby语言中内置的,而是数组变量的功能。

days = [ “Monday”,
天= [[星期一],
“Tuesday”,
“星期二”,
“Wednesday”,
“星期三”,
“Thursday”,
“星期四”,
“Friday”,
“星期五”,
“Saturday”,
“星期六”,
“Sunday”
“星期日”
]
]
days.each do|d|
天。每个| d |
puts d
放d
end
结束

使用索引运算符访问单个变量 ( Use the Index Operator to Access Individual Variables )

Beyond simple looping over an array—examining each individual variable in order—you can also access individual variables from an array using the index operator. The index operator will take a number and retrieve a variable from the array whose position in the array matches that number. Index numbers start at zero, so the first variable in an array has an index of zero.

除了简单遍历数组(按顺序检查每个变量)之外,您还可以使用索引运算符从数组访问单个变量。 索引运算符将获取一个数字,并从数组中检索一个变量,该变量在数组中的位置与该数字匹配。 索引号从零开始,因此数组中的第一个变量的索引为零。

So, for example, to retrieve the first variable from an array you can use array[0], and to retrieve the second you can use array[1]. In the following example, a list of names are stored in an array and are retrieved and printed using the index operator. The index operator can also be combined with the assignment operator to change the value of a variable in an array.

因此,例如,要从数组中检索第一个变量,可以使用array [0] ,而要检索第二个变量,可以使用array [1] 。 在以下示例中,名称列表存储在数组中,并使用索引运算符检索和打印。 索引运算符也可以与赋值运算符结合使用,以更改数组中变量的值。

names = [ “Bob”, “Jim”,
名称= [“鲍勃”,“吉姆”,
“Joe”, “Susan” ]
“乔”,“苏珊”]
puts names[0] # Bob
放名字[0]#鲍勃
puts names[2] # Joe
放名字[2]#乔
# Change Jim to Billy
#将Jim换成Billy
names[1] = “Billy”
names [1] =“比利”

翻译自: https://www.thoughtco.com/how-to-create-arrays-in-ruby-2908192

ruby 数组删除部分数组

发表评论

表情:
评论列表 (有 0 条评论,204人围观)

还没有评论,来说两句吧...

相关阅读