sort hash, hash key and val off by one, see attached

M

Mmcolli00 Mom

Please help me with this. I have been trying for days to get this right.
I don't see a next method for a hash. Just don't know what to do. Thanks
MC

Temp1.txt
----------
PersonID,PersonID
ID,ID
LastName,Social Security <need to set this val to "no valueâ€
Social Security, Address <set this key and value to Social Security..and
so on
Address,Unique ID
Unique ID,Potassium
----------

The LastName, Social Security line tells me that there is no value for
LastName and the next value in the list should be changed to the value
where Social Security instead of Address.

(my project consist of me comparing 2 files, there is more to it, but i
just wanted to explain what I am stumped on at this point.)

******************************************

File.open('Temp.txt', 'r+') do |temp|
rows = {}
temp.each_line do |line|
key, val = line.chomp.split(",",0)
rows[key] = val

#method to store first value
def storeKey(key,nextval = false)
storedKey = key
return storedKey if nextval == true
end

rows.each do |attrib|
if rows[key] != key then
@@newKey = rows[key].to_s
val1 = storeKey(@@newKey)
val2 = storeKey(@@newKey,true)
#if val == nextkey then replace with 'no value'
if rows[key] == val2 then
rows[key] = 'no value'
end
end
break
end
end

end

Attachments:
http://www.ruby-forum.com/attachment/3146/Temp1.txt
 
B

Brian Candler

Mmcolli00 said:
(my project consist of me comparing 2 files, there is more to it, but i
just wanted to explain what I am stumped on at this point.)

I'm afraid your problem makes absolutely no sense at all to me. You
could try explaining it more clearly, or give an example of the input
together with the output you expect to get for this entire input.

However I just wanted to point out to you a couple of things:
temp.each_line do |line| ...
#method to store first value
def storeKey(key,nextval = false)
storedKey = key
return storedKey if nextval == true
end

Ruby doesn't support nested methods. What you're actually doing here is
defining a new method *every* time this loop iterates. Better to move
this outside of the loop (and then it will probably become an instance
method of a class)
@@newKey = rows[key].to_s

You're using a class variable here, but it's not clear why, as you're
not inside any class definition; implicitly it ends up as that of the
'main' object. Much better to use a normal local variable. Even a global
variable ($newKey) would be better.

Also: hashes in ruby 1.8 are unordered. There is no 'next' method. If
there were (or if you iterate through all the elements using 'each')
you'd get them in an arbitrary order.

Regards,

Brian.
 
M

Mmcolli00 Mom

I figured it make no sense if I tried to explain it in a nutshell. I
just need to somehow find the next key in a hash and see if there is a
match with the current value. If so, then I could insert "no value" then
make all other lines equal so long as they are a match.

Instead of this...

PersonID,PersonID
ID,ID
LastName,Social Security
Social Security, Address
Address,Unique ID
Unique ID,Potassium

It would then look like this...

PersonID,PersonID
ID,ID
LastName,"no vale"
Social Security, Social Security
Address, Address,
Unique ID, Unique ID
Potassium, Potassium
 
D

Dave Bass

Mmcolli00 said:
I just need to somehow find the next key in a hash

I haven't read your example, but this statement rings alarm bells. You
need to define what you mean by "the next key". A hash has no natural
ordering -- it's just a bunch of key-value pairs -- so it's not obvious
what the "next" key means. The order in which you insert pairs is not
necessarily the order in which you get them back, and this order may
vary from one run to the next. This is basic to hashes.

If you don't mind the order in which the keys are processed, you can use
"each" to iterate through the hash.

As a quick fix, you could get all the keys into an array and sort them
into the order you want (alphabetically, numerically, etc).

Dave
 
D

David A. Black

Hi --

I haven't read your example, but this statement rings alarm bells. You
need to define what you mean by "the next key". A hash has no natural
ordering -- it's just a bunch of key-value pairs -- so it's not obvious
what the "next" key means. The order in which you insert pairs is not
necessarily the order in which you get them back, and this order may
vary from one run to the next. This is basic to hashes.

It's changing in 1.9, though. 1.9 hashes are ordered by key insertion
order.

$ ruby -ve 'p({1=>"hi", "a"=>"bye", []=>"whatever"})'
ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-darwin9.5.0]
{[]=>"whatever", "a"=>"bye", 1=>"hi"}

$ ruby19 -ve 'p({1=>"hi", "a"=>"bye", []=>"whatever"})'
ruby 1.9.1 (2008-12-30 patchlevel-0 revision 21203) [i386-darwin9.5.0]
{1=>"hi", "a"=>"bye", []=>"whatever"}


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
 
M

Mmcolli00 Mom

Thanks for the advice everyone. I think I might be getting somewhere
using an array instead since there is no 'next' in hashes. - new to
programming so I was really wasn't aware of that. Also thanks for the
tip on the new version and how hashes are ordered by key insertion. Good
Stuff!
MC ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top