我是Ruby编程的新手,目前正在从事自动化工作,我有以下阵列。
arr = [
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_personal_care_appliances"],
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_drugstore"],
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_pantry"],
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_pet_products"],
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_grocery"]
]
我想打印arr
的每个元素(数组)的最后一个元素,这里是"values gl_personal_care_appliances"
,"gl_drugstore"
,"gl_pantry"
,"gl_pet_products"
和{ {1}}。
我尝试了几种从网络获取参考的方法,但没有成功。任何线索或帮助
答案 0 :(得分:7)
如果数组是这样的:
<body>
<div>My Text</div>
</body>
然后,一个选项是构建一个包含所需元素的新数组:
array =[
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_personal_care_appliances"],
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_drugstore"],
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_pantry"],
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_pet_products"],
["DealsNEvents-9", "GLOBAL", "dom_validation_config", "4", "gl_grocery"]
]
或精简版:
array.map { |e| e.last }
# => ["gl_personal_care_appliances", "gl_drugstore", "gl_pantry", "gl_pet_products", "gl_grocery"]
但是,如果您只想打印字符串:
array.map(&:last)