arrays - 比较两个数组 Ruby 2.0
问题描述
我是学习 ruby 的新手,创建了两个数组。我正在尝试将数组 1 的每个元素与数组 2 进行比较,并打印数组 1 中不存在的元素和数组 2 中不存在的元素。
**#localarraydb**
bdlocal = CTrunk.find_by_sql('select phone_number from phones_trunks;')
connect = PG.connect(:hostaddr => @servers[0], :port => 5432, :dbname => "mydb", :user => "myuser", :connect_timeout => 90)
getdata = connect.exec("select name,active,phone_number from phones_trunks;")
array = []
getdata.each do |re|
array << re.values[2]
puts array
end
**#Local Array DB retrive each item from db**
bdlocal.each do |compare|
puts "Server 11:#{array[2]}\n Server Local:#{compare.phone_number}"
if compare == getdata then
puts "equals"
else
puts "different #{here show diferrence"
end
end
解决方案
你只需要减去它们:
array_1 = [1, 2, 3, 4]
array_2 = [3, 4, 5, 6]
array_1 - array_2
=> [1, 2]
array_2 - array_1
=> [5, 6]
推荐阅读
- sapui5 - sap.m.Carousel 的循环属性是否有效?
- apache-spark - 将 to_date 列与 pyspark 中的单个值进行比较
- matplotlib - 轴(子图)和/或 matplotlib.pyplot(plt)上的条件语句
- flutter - 跨路线提供者,但在 MaterialApp 之后
- nginx - nginx rewrite:重写并传递原始 URI 参数
- sed - 如何将 sed 保留空间附加到以后的匹配中?
- java - 如果我用 Java 生成二进制文件,如何用 C++ 读取它?
- javascript - Vue,基于href中的链接关闭首先完成的方法
- sql - 检索非空列值
- javascript - 从通过 CacheStorage api 通过 cache.match(req) 返回的响应中检索“时间缓存”