assoc_Ruby assoc()函数

assoc

Ruby中的assoc()函数 (assoc() function in Ruby)

We have studied functions to process single dimensional array so far but if we talk about assoc() function, it does not work for single dimensional arrays. assoc() function only works on Array of Arrays or you can say that it works on multidimensional arrays. The purpose of the assoc() function is to check the first element of each array. It processes array by checking the first element of the array with the specified index element, if the element is found then the whole array is returned otherwise the function returns nil or vacant.

到目前为止,我们已经研究了处理一维数组的函数,但是如果我们谈论assoc()函数 ,则该函数不适用于一维数组。 assoc()函数仅适用于数组数组,或者可以说它适用于多维数组。 assoc()函数的目的是检查每个数组的第一个元素。 它通过使用指定的索引元素检查数组的第一个元素来处理数组,如果找到该元素,则返回整个数组,否则该函数返回nil或vacant。

Syntax:

句法:

 Array_name.assoc(Object)

Now, let us understand the implementation of assoc() function with the help of examples broadly,

现在,让我们借助示例广泛地理解assoc()函数的实现,

Example 1:

范例1:

=begin
Ruby program to demonstrate implementation of assoc() function
=end

# Initializing multiple arrays of elements 
Arr1 = ["Fruits", "banana", "apple", "orange", "kiwi", "apricot", "Pineapple"] 
Arr2 = ["Languages", "C#", "Java", "Ruby", "Python", "C++","C"] 
Arr3 = ["Colors", "Red", "Brown", "Green", "Pink", "Yellow", "Teal"]
Arr4 = ["Vegetables", "Brinjal", "Tomato", "Potato", "Reddish"] 

# Creating a final array of above arrays 
FinalArray = [Arr1, Arr2, Arr3, Arr4] 

# Invoking assoc() function 
A1 = FinalArray.assoc("Fruits") 
A2 = FinalArray.assoc("Languages") 
A3 = FinalArray.assoc("Colors")
A4 = FinalArray.assoc("Vegetables") 

# Printing the matched contained array 
puts "#{A1}"
puts "#{A2}"
puts "#{A3}"
puts "#{A4}"

Output

输出量

["Fruits", "banana", "apple", "orange", "kiwi", "apricot", "Pineapple"]
["Languages", "C#", "Java", "Ruby", "Python", "C++", "C"]
["Colors", "Red", "Brown", "Green", "Pink", "Yellow", "Teal"]
["Vegetables", "Brinjal", "Tomato", "Potato", "Reddish"]

Code logic:

代码逻辑:

In the above example, we have initialized four arrays along with their categories as the first element of the array. Finalarray is storing these arrays as its elements. When the assoc() function is invoked with the element as an index, then the element is compared with the first element of each array. Arrays A1, A2, A3, and A4 are storing the contained array.

在上面的示例中,我们初始化了四个数组及其类别,作为数组的第一个元素。 Finalarray将这些数组作为元素存储。 当使用该元素作为索引调用assoc()函数时 ,该元素将与每个数组的第一个元素进行比较。 数组A1,A2,A3和A4正在存储包含的数组。

Example 2:

范例2:

=begin
Ruby program to demonstrate implementation of assoc() function
=end

# Initializing multiple arrays of elements 
Arr1 = ["Fruits", "banana", "apple", "orange", "kiwi", "apricot","Pineapple"] 
Arr2 = ["Languages", "C#", "Java", "Ruby", "Python","C++","C"] 
Arr3 = ["Colors", "Red", "Brown", "Green", "Pink","Yellow","Teal"]
Arr4 = ["Vegetables", "Brinjal", "Tomato", "Potato", "Raddish"] 

# Creating a final array of above arrays 
FinalArray = [Arr1, Arr2, Arr3, Arr4] 

# Taking input from user.
puts "Enter the catagory:-"
cat = gets.chomp
arr = FinalArray.assoc(cat)
if (arr==nil)
	puts "category not available"
else
	puts "The available elements under the category is:-"
puts "#{arr}"
end

Output

输出量

Run 1:-
Enter the category:-
Colors
The available elements under the category is:-
["Colors", "Red", "Brown", "Green", "Pink", "Yellow", "Teal"]

Run 2:-
Enter the category:-
Cars
category not available

Run 3:-
Enter the category:-
Fruits
The available elements under the category is:-
["Fruits", "banana", "apple", "orange", "kiwi", "apricot", "Pineapple"]

Code logic:

代码逻辑:

In this example, we have declared four arrays and then the final array. We are taking category as input from the user. We are passing that category to the assoc() function. If the category matches, the array is stored in the contained array otherwise assoc() is returning nil.

在此示例中,我们声明了四个数组,然后声明了最后一个数组。 我们将类别作为用户的输入。 我们将该类别传递给assoc()函数 。 如果类别匹配,则将数组存储在包含的数组中,否则assoc()返回nil。

翻译自: https://www.includehelp.com/ruby/assoc-function.aspx

assoc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值