#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Help with loading text file into hash


    Hi, I read from the stackoverflow forum that the hash works faster than the array. I am currently working on an assignment and I have managed to read a huge text file with array. However, it is not efficient, and the idea of using hash came into my mind. But I am not familiar with hash and would love to seek some advice and help on this.

    I have a method in a utility file that can't be amended. But this method will be called in the runcode file
    Code:
    def read_file(file)
    	lines = IO.readlines(file)
    	if lines != nil
    		for i in 0 .. lines.length-1
    			lines[i] = lines[i].sub("\n","")
    		end
    		return lines[0..lines.length-1]
    	end
    	return nil
    end
    In the runcode file, I have used array to read the text file and load the data into the array which is inefficient in terms of Big O notation.

    Code:
    def load(source_txt_file)
      read_file(source_txt_file).each{ |line|
        array = line.split(",")
    
        # current_line is the line that has just been read
        current_line = array[0].to_s
      
        if current_line == ""
          if $temp_array.size > 0
            $actor_movie_array << $temp_array
          end     
          $temp_array = []
        else
          $temp_array << current_line 
        end
      }
      if $temp_array.size > 0
        $actor_movie_array << $temp_array
        $temp_array = []
      end
    end
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    So it looks like the unalterable method you are given just removes "\n" characters from each line.

    Then, your method steps through all the lines and inserts non-blank lines into another array. Presumably, you want to look up lines in the other array without having to step through the entire array.

    A Hash contains key/value pairs, like "red"/1 and "green"/2. But you have no corresponding values for your lines, which would be the keys for the hash. So what you actually want is a Set. A set is essentially a Hash with no values. Here is an example:

    PHP Code:
    require 'set'

    Set.new
    << "hello world"
    << "goodbye mars"
    << "hello world"

    p s
    puts 
    'yes' if s.include? "goodbye mars"

    --output:--
    #<Set: {"hello world", "goodbye mars"}>
    yes 
    The s.include? part is 0(n).

    You can think of a Hash as an Array but instead of indexes 0...length_arr-1, the indexes can be anything you want. Strings are the most common thing that people use for the indexes (=the hash keys). Here is an example:

    PHP Code:
    person = {
      
    'first name' => "Joe",
      
    'last name' => "Blow"
    }

    puts person["first name"
    And you can think of a Set as a hash with all the values being True.
    Last edited by 7stud; March 14th, 2014 at 10:34 PM.

IMN logo majestic logo threadwatch logo seochat tools logo