1

I have the following text which will always follow the same format:

 1
 "13"
 "241"
 "Rabun"
 "06"
 "County"

 2
 "13"
 "281"
 "Towns"
 "06"
 "County"

I would like to assign each section to a hash like:

locality= {:id => "", :fips1 => "", :fips2 => "", :county => "", :stateid => "", :type => ""}

How would I go about doing this in Ruby? Any help is greatly appreciated.

4 Answers 4

1
fields = [:fips1,:fips2,:county,:stateid,:type]
arraywithhashes = yourtextdata.split("\n\n").map { |loc|
    Hash[
        [[:id,loc[/\d+/]]] +
        fields.zip(loc.scan(/"([^"]+)"/).map &:first)
    ]
}

If you add new fields to your file, the only you'll need to edit is to add it to fields.

Sign up to request clarification or add additional context in comments.

9 Comments

@willcodejavaforfood: Then learn it! After a while you'd rather starve than code Java...
The data is coming from a text file so if I do textfile = File.open("filename.dat") arraywithhashes = textfile.split("\n\n").map { |loc| I get a NoMethodError.
@Lars Haugseth - I'm trying to! Even joined a Open Source project :)
@Mike, file = File.open('filename.dat'); textfile = file.read; file.close or textfile = File.open('filename.dat'){|f|f.read} or textfile = File.read('filename.dat')
This just gives me the 1st text block of the text file.It doesn't seem to loop through the rest of the text blocks.
|
0

for each section, use a regular expression with groups corresponding to each entry in the section, then simply create hash table as you described from these groups.

Comments

0
locality.each_key { |k| locality.store(k, "foo") }

Another newbie-ish person here, but that might be a start for you.

Comments

0

You might want to consider using a Struct instead of a Hash.

Locality = Struct.new(:id, :fips1, :fips2, :county, :stateid, :type)
localities = []
DATA.each_slice(7) do |chunk|
    chunk.pop if chunk.size == 7 
    localities << Locality.new(*chunk.map{|line| line.scan(/\w+/) })
end

p localities # => [#<struct Locality id=["1"], fips1=["13"], fips2=["241"], etc. 
puts localities[1].fips2 # => 281

__END__
1
 "13"
 "241"
 "Rabun"
 "06"
 "County"

 2
 "13"
 "281"
 "Towns"
 "06"
 "County"
  • each_slice(7) takes 7 lines of DATA (the stuff after __END__ ).

  • The last line is removed unless there are only six lines (the last 'record').

  • A cleaned-up copy of the remaining lines is made. With these values a new Locality is created and added to an array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.