我正在探索结构。每个结构只有两个值:产品 ID 和数量。例如:
prodcode = 2473 , quantity = 1
prodcode = 2473 , quantity = 4
prodcode = 3328 , quantity = 6
prodcode = 2958 , quantity = 3
prodcode = 2473 , quantity = 7
prodcode = 2958 , quantity = 2
prodcode = 2473 , quantity = -1
我想得到这样的结构的数量总和:
prodcode = 2473 , quantity = 11
prodcode = 3328 , quantity = 6
prodcode = 2958 , quantity = 5
按数量降序排序将是一个加号。这是我用来创建有问题的结构数组的代码:
class Figs < Struct.new(:prodcode, :quantity)
def print_csv_record
prodcode.length==0 ? printf(",") : printf(prodcode)
quantity.length==0 ? printf(",") : printf(",", quantity.to_i)
printf("\n")
end
end
...
for x in 0...global.length
p = Figs.new
p.prodcode = global[x][0]
p.quantity = global[x][1].to_i
figures.push(p)
end
结构数组是“数字”,但我得到
undefined local variable or method 'quantity' for main:Object (NameError)