简体   繁体   中英

Problems with arrays and hashes in ruby

Im fighting with arrays and hashed and i need help I have an array with hashes, some thing like this

[{"a"=>1, "cool"=>1, "experiment"=>1, "is"=>1, "really"=>4, "this"=>1}, {"do"=>1, "it"=>4, "maybe"=>1, "think"=>1, "will"=>4, "work"=>2, "you"=>1}]

This is @analyzers

def initialize()
    @analyzers = Array.new
    @highest_count_across_lines = nil
    @highest_count_words_across_lines = nil
    @presol=nil
  end

  def analyze_file()
    File.foreach('test.txt').with_index(1) do |content, line|
      line_analyzer = LineAnalyzer.new(content, line)
      @analyzers << line_analyzer
    end
  end

  def calculate_line_with_highest_frequency()
    @highest_count_across_lines = analyzers.sort_by { |analyzer| analyzer.highest_wf_count }.reverse.first.highest_wf_count #This is 4 value, ok!
    @highest_count_words_across_lines = analyzers.select { |analyzer| @highest_count_across_lines == analyzer.highest_wf_count }

to get the highest_count_across_lines which is 4 as a number was not to complicated, i did it well but for the next method i need to get and array which contais the words [keys] with the value @highest_count_across_lines which is 4 and then

@highest_count_words_across_lines`

should be ["it", "really", "will"] Im trying doing this

@highest_count_words_across_lines = analyzers.select { |analyzer| @highest_count_across_lines == analyzer.highest_wf_count 

but I get nothing. I nned help to know how extract and array which contains the words with value= @highest_count_across_lines which is 4

Many thanks in advance

Im fighting with arrays and hashed and i need help I have an array with hashes, some thing like this

[{"a"=>1, "cool"=>1, "experiment"=>1, "is"=>1, "really"=>4, "this"=>1}, {"do"=>1, "it"=>4, "maybe"=>1, "think"=>1, "will"=>4, "work"=>2, "you"=>1}]

This is @analyzers

def initialize()
    @analyzers = Array.new
    @highest_count_across_lines = nil
    @highest_count_words_across_lines = nil
    @presol=nil
  end

  def analyze_file()
    File.foreach('test.txt').with_index(1) do |content, line|
      line_analyzer = LineAnalyzer.new(content, line)
      @analyzers << line_analyzer
    end
  end

  def calculate_line_with_highest_frequency()
    @highest_count_across_lines = analyzers.sort_by { |analyzer| analyzer.highest_wf_count }.reverse.first.highest_wf_count #This is 4 value, ok!
    @highest_count_words_across_lines = analyzers.select { |analyzer| @highest_count_across_lines == analyzer.highest_wf_count }

to get the highest_count_across_lines which is 4 as a number was not to complicated, i did it well but for the next method i need to get and array which contais the words [keys] with the value @highest_count_across_lines which is 4 and then

@highest_count_words_across_lines`

should be ["it", "really", "will"] Im trying doing this

@highest_count_words_across_lines = analyzers.select { |analyzer| @highest_count_across_lines == analyzer.highest_wf_count 

but I get nothing. I nned help to know how extract and array which contains the words with value= @highest_count_across_lines which is 4

Many thanks in advance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM