Ruby数组的问题

Ruby数组的问题,ruby,Ruby,我目前正在编写一个小的ruby类,该类用于存储数组中随机生成的数字出现的次数,以及出现的值本身 我正在尝试执行以下操作,以将值和默认时间视为1添加到数组中。脚本应检查该值是否已包含在数组中,如果已包含,则将该值的显示次数增加1 但是我接收到重复的值,这不应该发生-因为代码应该只允许一个值存储一次,如果该值已经在内存数组中,则将其递增一 代码附在下面,如果有人能建议我做错了什么,那就太棒了 干杯 马丁 完整的脚本不整洁 class STAT def initialize() #STAT me

我目前正在编写一个小的ruby类,该类用于存储数组中随机生成的数字出现的次数,以及出现的值本身

我正在尝试执行以下操作,以将值和默认时间视为1添加到数组中。脚本应检查该值是否已包含在数组中,如果已包含,则将该值的显示次数增加1

但是我接收到重复的值,这不应该发生-因为代码应该只允许一个值存储一次,如果该值已经在内存数组中,则将其递增一

代码附在下面,如果有人能建议我做错了什么,那就太棒了

干杯 马丁

完整的脚本不整洁

class STAT

def initialize()
  #STAT memory settings
  @memory = Array.new

  #Prediction settings
  @predictions = 0
  @sucessfulpredictions = 0
end

#STAT main functions
def store(value)

  foundflag = false
  @memory.each do |array|
    if value == array[0]

      #Incrementing value timesseen
      array[1] = array[1]+1

      #Value found, changing found flag
      foundflag = true

      #Loop break
      break
    end
  end

  if foundflag != true then
    @memory.push([value,1])
  end

end
def predict(nosimulations)

  #Generate random value less than the total memory size
  randomvalue = rand(total)+1

  count = 0
  @memory.each do |array|
    value = array[0]
    timesseen = array[1]
    if randomvalue <= count + timesseen
      puts "Predicted value #{value}"
    end
    count = count + array[1]
  end


end
def simulate(nosimulations)

  count = 1
  while count <= nosimulations
    #Generating a random number
    randomnumber = rand(100)+1
    #Storing the random number
    store(randomnumber)
    #Print simulation details#
    puts "Running simulation #{count} of #{nosimulations}"
    puts "Generated random number: #{randomnumber}"
    #Incrementing count
    count = count + 1
  end
end

#STAT technical functions
def inspect()
  #Print memory information message
  puts "Memory information:"

  @memory.each do |array|
    value = array[0]
    timesseen = array[1]
    puts "Value #{value} - times seen: #{timesseen}/#{total}"
  end
end
def total()
  total = 0
  @memory.each do |array|
    total = total + array[1]
  end
  return total
end

#STAT load/save functions
def load(filename)
  #Default engine to be loaded
  enginename = "#{filename}.stat"

  #Print checking for saved engine message
  puts "Checking for saved memory file..."

  #Checking for saved engine
  if File.exists?(enginename)
    #Print loading engine message
    puts "Loading memory..."

    @memory = Marshal.load File.read(enginename)

    #Print engine loaded message
    puts "Engine loaded"
  else
    #Print memory not found message
    puts "Cannot load memory, no memory file found"
  end
end
def save(filename)
  #Default name for engine to be saved
  enginename = "#{filename}.stat"

  #Print saving engine message
  puts "Saving memory..."

  #Saving engine to specified file
  serialized_array = Marshal.dump(@memory)
  savefile = File.new(enginename,"w") 
  savefile.write(serialized_array) 
  savefile.close

  #Print engine saved message
  puts "Memory saved"
end

end

#STAT class test software
stat = STAT.new
filename = "test"

#Load
stat.load(filename)

#Simulate
stat.simulate(1000000)

#Testing 
#stat.store(5)
#stat.store(5)

#Inspect
stat.inspect

#Predict
#stat.predict(1000000)

#Save
stat.save(filename)
使用:

获取哈希映射,将每个值映射到它出现的次数。要自己计算:

occurrences = Hash.new(0)
my_array.each{ |o| occurrences[o]+=1 }

看看你是否不知道这些方法的作用。

你工作太辛苦了。您希望存储在数组中看到随机生成的数字的次数,以及看到的值本身

Ruby怎么能帮你呢?因为您使用的是数组,所以浏览一下,是的,就是:count


您能展示演示如何获得副本的代码吗?因为我不能复制,而且我在你的代码中没有看到任何会导致重复的东西。完成sepp2k,如果你能看一看,这将是一个了不起的未来,如果你能产生一个最小的代码示例来演示这个问题,那将是很好的。如果不能,问题可能不是由您认为的地方引起的。作为旁注:在inspect中执行输出是个糟糕的主意。让inspect返回nil或其他非字符串的内容是绝对不能接受的。这会搞乱很多事情,比如irb。你应该用一个哈希来代替数组。然后只需执行@memory[value]| |=0@内存[value]+=1以存储值。不确定这是否解决了OP的需要;具体来说,他/她需要所有项目的计数,还是只需要一个特定项目?如果只需要一个,这是一个很好的答案。我需要能够存储看到生成的值的次数,以及值本身。哦,在大约1000万次模拟中,复制累积起来节省了MB。。这就是为什么我使用value×seen
occurrences = Hash[ my_array.group_by{ |o| o }.map{ |o,a| [o,a.length] } ]
occurrences = Hash.new(0)
my_array.each{ |o| occurrences[o]+=1 }
ar = [1,2,1,3,2,4,3,2,6,7,8,7]
rn = rand(10)
ar.count(rn)