Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How do you make a hash key using a wordy string?

Reply
Thread Tools

How do you make a hash key using a wordy string?

 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      12-03-2008
Hello everyone.

How do you make a hash key using a string? For instance,

txt file...
1 This is a string 22234343
2 This is another string 434355455
3 This is a different string 34324545


file = File.open("mytext4hash.txt")
file.each_line do |line|
rows = {}
key, val = line.chomp.split("""",4)
rows[key] = val

#this is where I am having issue.
puts rows["1 This is a string"] #need this to point to only "22234343"

end


Thanks in advance. MC
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Dean Wampler
Guest
Posts: n/a
 
      12-03-2008
Try this:

file = File.open("mytext4hash.txt")
rows = {}
file.each_line do |line|
/(.+)\s+(\d+)$/ =~ line.chomp
key = Regexp.last_match(1)
val = Regexp.last_match(2)
rows[key] = val
end
puts rows["1 This is a string"] #need this to point to only "22234343"

First, you were initializing "rows" inside the loop, which meant you
only kept the last line parsed! The regular expression does this:

(.+) matches at least one character (defaults to starting at the
beginning) and save it in the first "capture group". The parentheses
do that for you. I then refer to it on the next line; the "1" passed
to last_match.
\s+ matches at least one space.
(\d+)$ matches at least one digit at the end of the line ($) and
saves it as the second capture group.

Make sense?

dean


On Wed, Dec 3, 2008 at 8:09 AM, Mmcolli00 Mom <(E-Mail Removed)> wrote:
> Hello everyone.
>
> How do you make a hash key using a string? For instance,
>
> txt file...
> 1 This is a string 22234343
> 2 This is another string 434355455
> 3 This is a different string 34324545
>
>
> file = File.open("mytext4hash.txt")
> file.each_line do |line|
> rows = {}
> key, val = line.chomp.split("""",4)
> rows[key] = val
>
> #this is where I am having issue.
> puts rows["1 This is a string"] #need this to point to only "22234343"
>
> end
>
>
> Thanks in advance. MC
> --
> Posted via http://www.ruby-forum.com/.
>
>




--
Dean Wampler
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org

 
Reply With Quote
 
 
 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      12-03-2008
Thanks Dean. Makes sense!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
In 'HashMap.put', "if (e.hash == hash && eq(k, e.key))" ? Red Orchid Java 3 01-30-2006 07:04 PM



Advertisments