# Hash

**URL:** <https://rubytalk.org/t/hash/42762>\
**Category:** ruby-talk\
**Created:** [5 December 2007 14:41 UTC](https://rubytalk.org/t/hash/42762 "2007-12-05T14:41:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johnathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Johnathan\_Smith](https://rubytalk.org/u/Johnathan_Smith)\
**Post date:** [5 December 2007 14:41 UTC](https://rubytalk.org/t/hash/42762/1 "2007-12-05T14:41:59Z")

</div>

hi

im writing a class which so far:

- reads a text file  
- and prints out the the number of Tags it encounters

however now i want to create an empty hash every time it encounters a  
tag  
line, and if it encounters any other field, put the field and the  
related  
value in the hash, using the field name as the key

im unsure of how to go about this  
any help or pseudo code would be greatly appreciated  
my code is provided below

thanks

.txt file

Tag: ref1  
Type: Book  
Author: Little, S R

Tag: ref2  
Type: Journal  
Author: Smith, J

Tag: ref3  
Type: Conference Paper  
Author: Williams, M

ruby file:

> **···**
>
> #  
> #  
> &nbsp;&nbsp;require 'getoptlong'
> 
> &nbsp;&nbsp;opts = GetoptLong.new(  
> &nbsp;&nbsp;&nbsp;&nbsp;['--style', '-n', GetoptLong::NO\_ARGUMENT],  
> &nbsp;&nbsp;&nbsp;&nbsp;['--database', '-i', GetoptLong::REQUIRED\_ARGUMENT]  
> &nbsp;&nbsp;)
> 
> &nbsp;&nbsp;$linecount = 0
> 
> &nbsp;&nbsp;opts.each do |opt, arg|  
> &nbsp;&nbsp;&nbsp;&nbsp;case opt  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--style'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require arg  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--database'  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> #  
> #  
> #  
> # process options  
> #  
> #  
> #  
> &nbsp;&nbsp;File.open('reference.txt').each do |line|  
> &nbsp;&nbsp;&nbsp;&nbsp;if line =~ /^tag:/i  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$linecount += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;puts $linecount  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [5 December 2007 15:20 UTC](https://rubytalk.org/t/hash/42762/2 "2007-12-05T15:20:22Z")

</div>

Why do you repost the same question when you got answers already? Are  
those answers not propagated to ruby-forum?

Cheers

robert

> **···**
>
> 2007/12/5, Johnathan Smith \<stu\_09@hotmail.com\>:
> 
> > hi
> > 
> > im writing a class which so far:
> > 
> > - reads a text file  
> > - and prints out the the number of Tags it encounters
> > 
> > however now i want to create an empty hash every time it encounters a  
> > tag  
> > line, and if it encounters any other field, put the field and the  
> > related  
> > value in the hash, using the field name as the key
> > 
> > im unsure of how to go about this  
> > any help or pseudo code would be greatly appreciated  
> > my code is provided below
> > 
> > thanks
> > 
> > .txt file
> > 
> > Tag: ref1  
> > Type: Book  
> > Author: Little, S R
> > 
> > Tag: ref2  
> > Type: Journal  
> > Author: Smith, J
> > 
> > Tag: ref3  
> > Type: Conference Paper  
> > Author: Williams, M
> > 
> > ruby file:
> > 
> > #  
> > #  
> > &nbsp;&nbsp;require 'getoptlong'
> > 
> > &nbsp;&nbsp;opts = GetoptLong.new(  
> > &nbsp;&nbsp;&nbsp;&nbsp;['--style', '-n', GetoptLong::NO\_ARGUMENT],  
> > &nbsp;&nbsp;&nbsp;&nbsp;['--database', '-i', GetoptLong::REQUIRED\_ARGUMENT]  
> > &nbsp;&nbsp;)
> > 
> > &nbsp;&nbsp;$linecount = 0
> > 
> > &nbsp;&nbsp;opts.each do |opt, arg|  
> > &nbsp;&nbsp;&nbsp;&nbsp;case opt  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--style'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require arg  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--database'  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > #  
> > #  
> > #  
> > # process options  
> > #  
> > #  
> > #  
> > &nbsp;&nbsp;File.open('reference.txt').each do |line|  
> > &nbsp;&nbsp;&nbsp;&nbsp;if line =~ /^tag:/i  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$linecount += 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts $linecount  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Johnathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Johnathan\_Smith](https://rubytalk.org/u/Johnathan_Smith)\
**Post date:** [5 December 2007 15:26 UTC](https://rubytalk.org/t/hash/42762/3 "2007-12-05T15:26:14Z")

</div>

> Why do you repost the same question when you got answers already? Are  
> those answers not propagated to ruby-forum?

hi

i wasnt sure i had recieved the answers i was looking for. I was  
provided with a few different bits of code which im very grateful for  
but I was unable to get working. one of the solutions provided by the  
previous threat i will post below. Im not provided with any output at  
all. I was to maintain the tag count as well as creating the hash. if  
you could point out my errors id be extremly grateful

thanks

> **···**
>
> #  
> &nbsp;&nbsp;require 'getoptlong'
> 
> &nbsp;&nbsp;opts = GetoptLong.new(  
> &nbsp;&nbsp;&nbsp;&nbsp;['--style', '-n', GetoptLong::NO\_ARGUMENT],  
> &nbsp;&nbsp;&nbsp;&nbsp;['--database', '-i', GetoptLong::REQUIRED\_ARGUMENT]  
> &nbsp;&nbsp;)
> 
> &nbsp;&nbsp;$linecount = 0
> 
> &nbsp;&nbsp;opts.each do |opt, arg|  
> &nbsp;&nbsp;&nbsp;&nbsp;case opt  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--style'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require arg  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--database'  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> #  
> #  
> #  
> # process options  
> #  
> #  
> #  
> &nbsp;&nbsp;File.open('reference.txt').each do |line|  
> &nbsp;&nbsp;&nbsp;&nbsp;if line =~ /^tag:/i  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$linecount += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;puts $linecount
> 
> &nbsp;&nbsp;linecount = 0  
> &nbsp;&nbsp;results =   
> &nbsp;&nbsp;hash = {}  
> &nbsp;&nbsp;File.open('reference.txt').each do |line|  
> &nbsp;&nbsp;&nbsp;&nbsp;m = line.match /^(\w+):\s\*([\w+,\s]+)$/  
> &nbsp;&nbsp;&nbsp;&nbsp;unless m  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results \<\< hash unless hash.empty?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash = {}  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linecount += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash[m[1]] = m[2].chomp  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> #  
> #  
> #
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [5 December 2007 16:07 UTC](https://rubytalk.org/t/hash/42762/4 "2007-12-05T16:07:11Z")

</div>

If you don't understand or don't find an answer sufficient, say so in  
the same thread, no one will be mad. But if you start a new one, then  
people following the old one aren't helped if you get the answer in  
the new one.

Assuming that your text file is always in the format you listed, this  
may be what you're after...

database = {}  
File.open('reference.txt') { | handle |  
&nbsp;&nbsp;tags = handle.read.split("\n\n")  
&nbsp;&nbsp;for tag in tags  
&nbsp;&nbsp;&nbsp;&nbsp;key, type, author = tag.split("\n")  
&nbsp;&nbsp;&nbsp;&nbsp;database[key[5..-1]] = [type[6..-1], author[9..-1]]  
&nbsp;&nbsp;end  
}  
p database

Given your sample data, this will output the following...

{"ref1"=\>["Book", "Little, S R"], "ref2"=\>["Journal", "Smith, J"],  
"ref3"=\>["Conference Paper", "Williams, M"]}

And the number of tags seen is just "database.length".

Regards,  
Jordan

> **···**
>
> On Dec 5, 9:26 am, Johnathan Smith \<stu...@hotmail.com\> wrote:
> 
> > \> Why do you repost the same question when you got answers already? Are  
> > \> those answers not propagated to ruby-forum?
> > 
> > hi
> > 
> > i wasnt sure i had recieved the answers i was looking for. I was  
> > provided with a few different bits of code which im very grateful for  
> > but I was unable to get working. one of the solutions provided by the  
> > previous threat i will post below. Im not provided with any output at  
> > all. I was to maintain the tag count as well as creating the hash. if  
> > you could point out my errors id be extremly grateful
> > 
> > thanks
> > 
> > #  
> > &nbsp;&nbsp;require 'getoptlong'
> > 
> > &nbsp;&nbsp;opts = GetoptLong.new(  
> > &nbsp;&nbsp;&nbsp;&nbsp;['--style', '-n', GetoptLong::NO\_ARGUMENT],  
> > &nbsp;&nbsp;&nbsp;&nbsp;['--database', '-i', GetoptLong::REQUIRED\_ARGUMENT]  
> > &nbsp;&nbsp;)
> > 
> > &nbsp;&nbsp;$linecount = 0
> > 
> > &nbsp;&nbsp;opts.each do |opt, arg|  
> > &nbsp;&nbsp;&nbsp;&nbsp;case opt  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--style'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require arg  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when '--database'  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > #  
> > #  
> > #  
> > # process options  
> > #  
> > #  
> > #  
> > &nbsp;&nbsp;File.open('reference.txt').each do |line|  
> > &nbsp;&nbsp;&nbsp;&nbsp;if line =~ /^tag:/i  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$linecount += 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts $linecount
> > 
> > &nbsp;&nbsp;linecount = 0  
> > &nbsp;&nbsp;results =   
> > &nbsp;&nbsp;hash = {}  
> > &nbsp;&nbsp;File.open('reference.txt').each do |line|  
> > &nbsp;&nbsp;&nbsp;&nbsp;m = line.match /^(\w+):\s\*([\w+,\s]+)$/  
> > &nbsp;&nbsp;&nbsp;&nbsp;unless m  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results \<\< hash unless hash.empty?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash = {}  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linecount += 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash[m[1]] = m[2].chomp  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > #  
> > #  
> > #
> > 
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Johnathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Johnathan\_Smith](https://rubytalk.org/u/Johnathan_Smith)\
**Post date:** [5 December 2007 20:28 UTC](https://rubytalk.org/t/hash/42762/5 "2007-12-05T20:28:05Z")

</div>

hi,

thanks, i've got it to print out the hash  
although I'm getting a wierd output where its taking the first letter  
off the surname e.g:

> ruby main.rb

3  
{"ref1"=\>["Book", "ittle, S R"], "ref2"=\>["Journal", "mith, J"]

can you see in my code where im going wrong?  
also, is it possible to print the different refrences on new lines?

ill provide my code below  
thanks

> **···**
>
> #  
> #  
> &nbsp;&nbsp;File.open('reference.txt').each do |line|  
> &nbsp;&nbsp;&nbsp;&nbsp;if line =~ /^tag:/i  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$linecount += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;puts $linecount  
> #  
> #  
> #  
> &nbsp;&nbsp;File.open('reference.txt') { | handle |tags =  
> handle.read.split("\n\n")  
> &nbsp;&nbsp;&nbsp;&nbsp;for tag in tags  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key, type, author = tag.split("\n")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database[key[5..-1]] = [type[6..-1], author[9..-1]]  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;}  
> &nbsp;&nbsp;p database  
> #  
> #  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [5 December 2007 20:50 UTC](https://rubytalk.org/t/hash/42762/6 "2007-12-05T20:50:04Z")

</div>

> hi,
> 
> thanks, i've got it to print out the hash  
> although I'm getting a wierd output where its taking the first letter  
> off the surname e.g:
> 
> \>ruby main.rb
> 
> 3  
> {"ref1"=\>["Book", "ittle, S R"], "ref2"=\>["Journal", "mith, J"]
> 
> can you see in my code where im going wrong?  
> also, is it possible to print the different refrences on new lines?
> 
> ill provide my code below  
> thanks
> 
> #  
> #  
> &nbsp;&nbsp;File.open('reference.txt').each do |line|  
> &nbsp;&nbsp;&nbsp;&nbsp;if line =~ /^tag:/i  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$linecount += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;puts $linecount  
> #  
> #  
> #

^ This part is pointless now. $linecount above is equal to  
database.length in the code below.

> &nbsp;&nbsp;File.open('reference.txt') { | handle |tags =  
> handle.read.split("\n\n")  
> &nbsp;&nbsp;&nbsp;&nbsp;for tag in tags  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key, type, author = tag.split("\n")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database[key[5..-1]] = [type[6..-1], author[9..-1]]  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;}  
> &nbsp;&nbsp;p database  
> #  
> #  
> --  
> Posted viahttp://www.ruby-forum.com/.

It should have been author[8..-1], sorry about that.

I suggest that you go through one of the many ruby guides/tutorials  
listed here: [Documentation](http://www.ruby-lang.org/en/documentation/)

Regards,  
Jordan

> **···**
>
> On Dec 5, 2:28 pm, Johnathan Smith \<stu...@hotmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Johnathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Johnathan\_Smith](https://rubytalk.org/u/Johnathan_Smith)\
**Post date:** [5 December 2007 21:00 UTC](https://rubytalk.org/t/hash/42762/7 "2007-12-05T21:00:38Z")

</div>

cheers.

yer i have been reading many tutorials but was unable to grasp what I  
was trying to achieve.

one for thing i was wondering:

if i was to extend the some of the information in the .txt file  
e.g:

Tag: ref1  
Type: Book  
Author: Smith, J  
Publisher: New Books  
Chapter: 12

could i adapt the code so "any other information" is put into the hash  
rather than having set information like author or type?

thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [5 December 2007 22:08 UTC](https://rubytalk.org/t/hash/42762/8 "2007-12-05T22:08:52Z")

</div>

Yes, both of the pieces of code I wrote for you originally handle this  
case just fine. Please read through the code and understand it. You  
asked for pseudo-code originally, and I actually gave you fully-  
functioning code. Now you just need to understand how to use it and  
its output.

> **···**
>
> On Dec 5, 2:00 pm, Johnathan Smith \<stu...@hotmail.com\> wrote:
> 
> > yer i have been reading many tutorials but was unable to grasp what I  
> > was trying to achieve.
> > 
> > one for thing i was wondering:
> > 
> > if i was to extend the some of the information in the .txt file  
> > e.g:
> > 
> > Tag: ref1  
> > Type: Book  
> > Author: Smith, J  
> > Publisher: New Books  
> > Chapter: 12
> > 
> > could i adapt the code so "any other information" is put into the hash  
> > rather than having set information like author or type?

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [5 December 2007 22:19 UTC](https://rubytalk.org/t/hash/42762/9 "2007-12-05T22:19:59Z")

</div>

Sorry about that Phrogz. I didn't read the other thread, I just  
glanced at it briefly the other day. I didn't realize you had already  
answered this fully.

Regards,  
Jordan

> **···**
>
> On Dec 5, 4:02 pm, Phrogz \<phr...@mac.com\> wrote:
> 
> > On Dec 5, 2:00 pm, Johnathan Smith \<stu...@hotmail.com\> wrote:
> > 
> > \> yer i have been reading many tutorials but was unable to grasp what I  
> > \> was trying to achieve.
> > 
> > \> one for thing i was wondering:
> > 
> > \> if i was to extend the some of the information in the .txt file  
> > \> e.g:
> > 
> > \> Tag: ref1  
> > \> Type: Book  
> > \> Author: Smith, J  
> > \> Publisher: New Books  
> > \> Chapter: 12
> > 
> > \> could i adapt the code so "any other information" is put into the hash  
> > \> rather than having set information like author or type?
> > 
> > Yes, both of the pieces of code I wrote for you originally handle this  
> > case just fine. Please read through the code and understand it. You  
> > asked for pseudo-code originally, and I actually gave you fully-  
> > functioning code. Now you just need to understand how to use it and  
> > its output.

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [5 December 2007 22:35 UTC](https://rubytalk.org/t/hash/42762/10 "2007-12-05T22:35:03Z")

</div>

Absolutely no need for an apology. Helping people isn't a competition  
of who can answer first. And, if I choose to stop helping someone  
because I've done as much as I'm willing to do, there's certainly no  
etiquette that says you shouldn't step in.

I'm glad you helped Johnathan get further towards his solution that I  
did, and certainly I don't care that you used your own code versus  
mine.

But I appreciate the sentiment. 🙂

> **···**
>
> On Dec 5, 3:19 pm, MonkeeSage \<MonkeeS...@gmail.com\> wrote:
> 
> > On Dec 5, 4:02 pm, Phrogz \<phr...@mac.com\> wrote:  
> > \> Yes, both of the pieces of code I wrote for you originally handle this  
> > \> case just fine. Please read through the code and understand it. You  
> > \> asked for pseudo-code originally, and I actually gave you fully-  
> > \> functioning code. Now you just need to understand how to use it and  
> > \> its output.
> > 
> > Sorry about that Phrogz. I didn't read the other thread, I just  
> > glanced at it briefly the other day. I didn't realize you had already  
> > answered this fully.

---

<div class="post-metadata">

**Author:** ![Johnathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Johnathan\_Smith](https://rubytalk.org/u/Johnathan_Smith)\
**Post date:** [5 December 2007 22:49 UTC](https://rubytalk.org/t/hash/42762/11 "2007-12-05T22:49:38Z")

</div>

hey guys

sorry for the whole mix up! I appreciate all the help I've received  
especially as im new to ruby! im also new to the forum and should have  
tried to stick it our with the first threat! so i hope i havn't offended  
any of you by this.

that aside, i really hope you can help me resolve this problem.

Jordan, the code you gave me worked successfully. However, it just deals  
with the the two types i gave you, author and type.Its partially my  
fault for not explaining properly becuase i hope it could deal with any  
information after the tag. is this possible to achieve?

again sorry for the mix up  
cheers

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [6 December 2007 00:10 UTC](https://rubytalk.org/t/hash/42762/12 "2007-12-06T00:10:01Z")

</div>

> hey guys
> 
> sorry for the whole mix up! I appreciate all the help I've received  
> especially as im new to ruby! im also new to the forum and should have  
> tried to stick it our with the first threat! so i hope i havn't offended  
> any of you by this.

Nah! Any mistake you might make, we've already done that and worse  
(probably several times). 🙂

> that aside, i really hope you can help me resolve this problem.
> 
> Jordan, the code you gave me worked successfully. However, it just deals  
> with the the two types i gave you, author and type.Its partially my  
> fault for not explaining properly becuase i hope it could deal with any  
> information after the tag. is this possible to achieve?

Sure it is. The code I gave you can be expanded, but it was mainly  
just for you to get started with, because it is fragile (as you saw  
accidentally leave out the space between the "Author: Name" line, it  
will still clip the first letter off the name ([number..number] means  
to grab a smaller section from a larger container, and -1 has the  
special meaning of the last item; so author[8..-1] means to get the  
characters from number 8 to the end).

What you really want is something more flexible, that will allow for  
those kinds of mistakes and still do the right thing in almost every  
case. That is a regular expression. It lets you give it a pattern  
which has certain rules, and matches a string in different ways based  
on those rules. And this is just what Phrogz' code is doing:

# we'll get rid of linecount since you can  
# get that information directly from your hash

# and we'll store the hashes as subhashes  
# underneath a main one, keyed by "Tag:" value

database = {}

File.open('reference.txt') { | handle |

&nbsp;&nbsp;last\_tag = nil # we'll use this to keep track of  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the last tag we've seen, see below

&nbsp;&nbsp;handle.each { | line |

&nbsp;&nbsp;&nbsp;&nbsp;# the following regexp says to match:  
&nbsp;&nbsp;&nbsp;&nbsp;# ^ = starting at the first char  
&nbsp;&nbsp;&nbsp;&nbsp;# (\w+) = 1 or more "word" characters (a-z / 0-9)  
&nbsp;&nbsp;&nbsp;&nbsp;# : = followed by a literal colon  
&nbsp;&nbsp;&nbsp;&nbsp;# \s\* = then zero or more "space" characters  
&nbsp;&nbsp;&nbsp;&nbsp;# ([\w+,\s]+) = 1 or more "word/space/," characters  
&nbsp;&nbsp;&nbsp;&nbsp;# $ = with nothing else until the end of line

> **···**
>
> On Dec 5, 4:49 pm, Johnathan Smith \<stu...@hotmail.com\> wrote:  
> from my mis-post). Even with the correct "author[8..-1]", if you  
> &nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;# any string meeting that criteria is a match and m is  
> &nbsp;&nbsp;&nbsp;&nbsp;# the match data, however if the string doesn't match  
> &nbsp;&nbsp;&nbsp;&nbsp;# e.g., a blank line, m is nil
> 
> &nbsp;&nbsp;&nbsp;&nbsp;m = line.match(/^(\w+):\s\*([\w+,\s]+)$/)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;if m # if m is a match (i.e., not nil)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if m[1] == 'Tag' # we want to add a key to the hash  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# since this is a "Tag:" line  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# when we used the () in the regexp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# that told ruby we only care about  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# those chars, and we can access  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# them in the match object with m[n]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# i.e, m[1] means whatever part of the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# line matched (\w+)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last\_tag = m[2].chomp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database[last\_tag] = {} # make a subhash as the value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# now we have, e.g.,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# database["ref1"] = {}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# it's not a "Tag:" line, so we add it as key, value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# to the subhash we created for the last tag key we  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# created in the hash
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database[last\_tag][m[1].downcase] = m[2].chomp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# e.g., database["ref1"]["type"] = "Journal"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;}  
> }
> 
> # now print it  
> print "# of Tags: ", database.length, "\n\n"  
> print "Contents: ", database.inspect, "\n\n"
> 
> puts database["ref2"]["author"]
> 
> ========
> 
> This outputs:
> 
> # of Tags: 3
> 
> Contents: {"ref1"=\>{"author"=\>"Little, S R", "type"=\>"Book"},  
> "ref2"=\>{"author"=\>"Smith, J", "type"=\>"Journal"},  
> "ref3"=\>{"author"=\>"Williams, M", "type"=\>"Conference Paper"}}
> 
> Smith, J
> 
> > again sorry for the mix up  
> > cheers
> > 
> > --  
> > Posted viahttp://www.ruby-forum.com/.
> 
> No worries 🙂
> 
> HTH,  
> Jordan

---

<div class="post-metadata">

**Author:** ![Johnathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Johnathan\_Smith](https://rubytalk.org/u/Johnathan_Smith)\
**Post date:** [6 December 2007 00:46 UTC](https://rubytalk.org/t/hash/42762/13 "2007-12-06T00:46:11Z")

</div>

thanks mate.

i really appreciate it. and its making sense!  
although i have one conern at the end of the output it prints J, Smith  
randomly

can this be avoided?

thanks again

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Johnathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Johnathan\_Smith](https://rubytalk.org/u/Johnathan_Smith)\
**Post date:** [6 December 2007 00:54 UTC](https://rubytalk.org/t/hash/42762/14 "2007-12-06T00:54:52Z")

</div>

> although i have one conern at the end of the output it prints J, Smith  
> randomly

oh wait...iv solved it  
guess it shows i shud actually have a go at it properly

cheers

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
