Hello,
I have a question, I want to store some data in two columns, for example
0001, stuff1 stuff2
0002, morestuff
0003, extrastuff
0004, more evenmore
As you can see column 1 is an ID and column 2 is one or more strings.
I want to do a search across another data set to say if column 1 (ID
number) matches in both sets and the contents of column 2 to data set B.
You can do like this to obtain the keys that are common in both sets,
then iterate them doing something with the values (in my example add
the values from the second to the first, not sure if that's what you
want):
irb(main):001:0> h1 = Hash[1,"a",2,"b",3,"c"]
=> {1=>"a", 2=>"b", 3=>"c"}
irb(main):002:0> h2 = Hash[1,"x",3,"y"]
=> {1=>"x", 3=>"y"}
irb(main):004:0> (h1.keys & h2.keys).each {|key| h1[key] += h2[key]}
=> [1, 3]
irb(main):005:0> h1
=> {1=>"ax", 2=>"b", 3=>"cy"}
I initially thought to use a hash, but it scrambles the order of the
data so was unsure of its efficiency and use.
If you want to keep the order then you will need to sort the keys, but
you can do that after the operation. About efficiency: the hash is
good for key access (amortized O(1)), although in this case it only
helps in the last step (adding the values), as you need to make the
intersection of all the keys first.
Hope this helps,
Jesus.
···
On Mon, Sep 21, 2009 at 6:05 PM, Ne Scripter <stuart.clarke@northumbria.ac.uk> wrote: