# Sequentially output each hash key and value

**URL:** <https://rubytalk.org/t/sequentially-output-each-hash-key-and-value/18995>\
**Category:** ruby-talk\
**Created:** [15 June 2005 23:00 UTC](https://rubytalk.org/t/sequentially-output-each-hash-key-and-value/18995 "2005-06-15T23:00:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_L\_Snyder1](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Charles\_L\_Snyder1](https://rubytalk.org/u/Charles_L_Snyder1)\
**Post date:** [15 June 2005 23:00 UTC](https://rubytalk.org/t/sequentially-output-each-hash-key-and-value/18995/1 "2005-06-15T23:00:32Z")

</div>

Hi,

I have a snippet of code which

1. asks for a title from user  
2. opens a text file of the form :

definition: blah blah blah  
incidence: blah yah blah  
category: yeah blah tah  
..  
..

3. puts everything to the left of the : into a hash as the keys, and  
everything to the right in as matching values.  
4. Outputs it as "Title.htm" file in format I want

My code works for (obviously) the key and value ONLY in the 1 position -  
Problem is I can't figure out how to get it to sequentially iterate thru the  
whole hash table - should be easy, but I can't make it work... Thanks in  
advance !

Here is my code:

p "Enter a title..."  
title = gets.chomp  
talks\_text =   
dict = {}  
IO.foreach("testfile.txt") do

> x\>

if  
y = /:/.match(x)  
y = y.pre\_match  
z=/:/.match(x)  
z = z.post\_match  
dict[y] = z  
end  
end  
dict.sort

File::open(title + '.htm', 'w') do |f|  
f.puts "\<HTML\>\n\<HEAD\>\<TITLE\>" + title + "\</TITLE\>\</HEAD\>\n\<BODY  
bgcolor=#FFDEAD\>\n"  
f.puts "\<CENTER\>\<A NAME=\"page\_top\"\>\<H1\>" + title + "\</H1\>\</A\>"  
&nbsp;&nbsp;f.puts "by Charles L. Snyder, MD\<BR\>\n"  
f.puts "\</CENTER\>\n\<HR\>\n"  
&nbsp;&nbsp;f.puts "\<A NAME=\"$anchor\"\>\<H2\>" + dict.keys[1] + "\</H2\>\</A\>" +  
dict.values[1] + "\<BR\>"  
f.puts "\<CENTER\>\<A HREF=\'javascript:window.history.back()\'\>Back\</A\>&nbsp"  
&nbsp;&nbsp;f.puts "\</CENTER\>\n\<HR\>\n"  
end

CLS

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@Tim\_Hunter](https://rubytalk.org/u/Tim_Hunter)\
**Post date:** [15 June 2005 23:10 UTC](https://rubytalk.org/t/sequentially-output-each-hash-key-and-value/18995/2 "2005-06-15T23:10:32Z")

</div>

Charles L. Snyder wrote:

> My code works for (obviously) the key and value ONLY in the 1 position -  
> Problem is I can't figure out how to get it to sequentially iterate thru the  
> whole hash table - should be easy, but I can't make it work... Thanks in  
> advance !

Hash#each

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [16 June 2005 10:15 UTC](https://rubytalk.org/t/sequentially-output-each-hash-key-and-value/18995/3 "2005-06-16T10:15:33Z")

</div>

Charles L. Snyder wrote:

> Hi,
> 
> I have a snippet of code which
> 
> 1. asks for a title from user  
> 2. opens a text file of the form :
> 
> definition: blah blah blah  
> incidence: blah yah blah  
> category: yeah blah tah  
> .  
> .
> 
> 3. puts everything to the left of the : into a hash as the keys, and  
> everything to the right in as matching values.  
> 4. Outputs it as "Title.htm" file in format I want
> 
> My code works for (obviously) the key and value ONLY in the 1  
> position

What do you mean by that?

> - Problem is I can't figure out how to get it to  
> sequentially iterate thru the whole hash table - should be easy, but  
> I can't make it work... Thanks in advance !
> 
> Here is my code:
> 
> p "Enter a title..."  
> title = gets.chomp  
> talks\_text =   
> dict = {}  
> IO.foreach("testfile.txt") do
> 
> > x\>
> 
> if  
> y = /:/.match(x)  
> y = y.pre\_match  
> z=/:/.match(x)  
> z = z.post\_match  
> dict[y] = z  
> end  
> end

Reading can be somewhat simplified:

# untested  
IO.foreach("testfile.txt") do |line|  
&nbsp;&nbsp;if /^\s\*(w+)\s\*:\s\*\b(.\*)$/ =~ line  
&nbsp;&nbsp;&nbsp;&nbsp;dict[$1] = $2  
&nbsp;&nbsp;end  
end

> dict.sort

This sort is useless as it will not sort the Hash but return an ordered  
array of arrays. You're only wasting CPU cycles here.

> File::open(title + '.htm', 'w') do |f|  
> f.puts "\<HTML\>\n\<HEAD\>\<TITLE\>" + title + "\</TITLE\>\</HEAD\>\n\<BODY  
> bgcolor=#FFDEAD\>\n"  
> f.puts "\<CENTER\>\<A NAME=\"page\_top\"\>\<H1\>" + title + "\</H1\>\</A\>"  
> &nbsp;&nbsp;f.puts "by Charles L. Snyder, MD\<BR\>\n"  
> f.puts "\</CENTER\>\n\<HR\>\n"  
> &nbsp;&nbsp;f.puts "\<A NAME=\"$anchor\"\>\<H2\>" + dict.keys[1] + "\</H2\>\</A\>" +  
> dict.values[1] + "\<BR\>"  
> f.puts "\<CENTER\>\<A  
> &nbsp;&nbsp;HREF=\'javascript:window.history.back()\'\>Back\</A\>&nbsp" f.puts  
> "\</CENTER\>\n\<HR\>\n" end

File::open(title + '.htm', 'w') do |f|  
f.puts "\<HTML\>\n\<HEAD\>\<TITLE\>" + title + "\</TITLE\>\</HEAD\>\n\<BODY  
bgcolor=#FFDEAD\>\n"  
f.puts "\<CENTER\>\<A NAME=\"page\_top\"\>\<H1\>" + title + "\</H1\>\</A\>"  
&nbsp;&nbsp;f.puts "by Charles L. Snyder, MD\<BR\>\n"  
&nbsp;&nbsp;f.puts "\</CENTER\>\n\<HR\>\n"

&nbsp;&nbsp;dict.sort.each do |key,value|  
&nbsp;&nbsp;&nbsp;&nbsp;f.puts "\<A NAME=\"$anchor\"\>\<H2\>" + key + "\</H2\>\</A\>" + value + "\<BR\>"  
&nbsp;&nbsp;end

f.puts "\<CENTER\>\<A  
HREF=\'javascript:window.history.back()\'\>Back\</A\>&nbsp"  
&nbsp;&nbsp;f.puts "\</CENTER\>\n\<HR\>\n"  
end

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert
