[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

sequentially output each hash key and value

Charles L. Snyder

6/15/2005 10:58:00 PM

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>"
f.puts "by Charles L. Snyder, MD<BR>\n"
f.puts "</CENTER>\n<HR>\n"
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"
f.puts "</CENTER>\n<HR>\n"
end



CLS


2 Answers

Tim Hunter

6/15/2005 11:10:00 PM

0

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

Robert Klemme

6/16/2005 10:12:00 AM

0

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|
if /^\s*(w+)\s*:\s*\b(.*)$/ =~ line
dict[$1] = $2
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>"
> f.puts "by Charles L. Snyder, MD<BR>\n"
> f.puts "</CENTER>\n<HR>\n"
> 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" 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>"
f.puts "by Charles L. Snyder, MD<BR>\n"
f.puts "</CENTER>\n<HR>\n"

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

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


Kind regards

robert