Group by unique entries of a hash

I have two data sets loaded into a hash to give the following output

“2efa4ba470”, “00000005”
“2efa4ba470”, “00000004”
“02adecfd5c”, “00000002”
“c0784b5de101”, “00000006”
“68c4bf10539”, “00000003”
“c0784b5de101”, “00000001”

My code to get this is as follows:

source= “C:\dummyFile.txt”
hashMapping = Hash.new
ocrIDMapping = Hash.new

IO.foreach(source.to_s) do |data|
fields = data.split(",")
hash = fields[0]
ocrID = fields[1]
hashMapping[ocrID] = hash
end

hashMapping.sort{|a,b| a[1]<=>b[1]}.each { |elem|

puts “#{elem[1]}, #{elem[0]}”}

I would like to alter my output to group my the first value to give an
output like this:

“2efa4ba470”, “00000005”, “00000004”
“02adecfd5c”, “00000002”
“c0784b5de101”, “00000006”, “00000001”
“68c4bf10539”, “00000003”

As you can see now only unique values are shown in the first field
however a list of the corresponding second field is formed, grouping the
results. Something like this I could do in SQL however I have never come
across it in Ruby so does anyone have any pointers?

Many thanks

On Tue, Sep 29, 2009 at 12:43 PM, Ne Scripter
[email protected] wrote:

“68c4bf10539”, “00000003”

As you can see now only unique values are shown in the first field
however a list of the corresponding second field is formed, grouping the
results. Something like this I could do in SQL however I have never come
across it in Ruby so does anyone have any pointers?

You want a hash where the key is the element you want to group on, and
the ‘item’ is an array of all items with the shared key. A bit like
(untested):

hashMapping = {}

IO.foreach(source.to_s) do |data|
fields = data.split(",")
hash = fields[0]
ocrID = fields[1]

hashMapping[ocrID] ||= [] #If hashMapping has never seen this key
before, make an empty array

hashMapping[ocrID] << hash #Add the new element to the array for this
key

end

Many thanks

Posted via http://www.ruby-forum.com/.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

2009/9/29 Paul S. [email protected]:

hashMapping[ocrID] = hash
“02adecfd5c”, “00000002”
(untested):

hashMapping[ocrID] << hash #Add the new element to the array for this key

end

It is slightly more efficient to do it in one step:

(hashMapping[ocrID] ||= []) << hash

Even nicer

hashMapping = Hash.new {|h,k| h[k] = []}

hashMapping[ocrID] << hash

Kind regards

robert

On Tue, Sep 29, 2009 at 4:22 PM, Paul S. [email protected]
wrote:

On Tue, Sep 29, 2009 at 2:38 PM, Robert K.
[email protected] wrote:

Even nicer

hashMapping = Hash.new {|h,k| h[k] = []}

Is this defining a default element for the hash? I had a vague
recollection you could do this but completely forgot how.

Using that constructor you pass a block which will be executed every
time there’s a missing key. The value of the block is used as a
default value, but as it is in this case, it can have the side effect
of modifying the hash. If you don’t modify the hash inside the block,
it’s not modified as you can see in the first example:

Two examples:

irb(main):001:0> h = Hash.new {|h,k| 0}
=> {}
irb(main):002:0> h[:a]
=> 0
irb(main):003:0> h[:a] += 1
=> 1
irb(main):004:0> h
=> {:a=>1}
irb(main):005:0> h2 = Hash.new {|h,k| h[k] = []}
=> {}
irb(main):007:0> h2[:a]
=> []
irb(main):008:0> h2
=> {:a=>[]}

Jesus.

2009/9/29 Paul S. [email protected]:

On Tue, Sep 29, 2009 at 2:38 PM, Robert K.
[email protected] wrote:

2009/9/29 Paul S. [email protected]:

On Tue, Sep 29, 2009 at 12:43 PM, Ne Scripter
[email protected] wrote:

hashMapping[ocrID] << hash #Add the new element to the array for this key

Is this defining a default element for the hash? I had a vague
recollection you could do this but completely forgot how.

No, this is defining a hook which is executed each time a key is
requested which is not present. In this case the hook stores a new
Array in the Hash but you could do other things as well.

A default value is defined via Hash.new([]) which does not work in
this case for obvious reasons.

I’d also rename the ‘hash’ variable to ‘key’ or something, I think
it’s less confusing. Then Your hashMapping can either be given the
name ‘hash’, because that’s what it is, or a name that’s actually
useful for describing what the mystical contents of the hash are.

Absolutely. I just did not want to cause extra confusion by starting
to rename everything. :slight_smile:

Cheers

robert

On Tue, Sep 29, 2009 at 2:38 PM, Robert K.
[email protected] wrote:

“c0784b5de101”, “00000001”
ocrID = fields[1]
“2efa4ba470”, “00000005”, “00000004”
the ‘item’ is an array of all items with the shared key. A bit like
before, make an empty array

hashMapping = Hash.new {|h,k| h[k] = []}

Is this defining a default element for the hash? I had a vague
recollection you could do this but completely forgot how.

I’d also rename the ‘hash’ variable to ‘key’ or something, I think
it’s less confusing. Then Your hashMapping can either be given the
name ‘hash’, because that’s what it is, or a name that’s actually
useful for describing what the mystical contents of the hash are.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

So if we were to take this further.We now have out hashMapping list

“2efa4ba470”, "00000005"“00000004”
“c0784b5de101”, "00000006"“00000003”
“02adecfd5c”, “00000002”
“c0784b5de101”, “00000001”

Now we split the hash to give only the ID values (column 2) again doing
some like this:

hashMapping.each do |itemDetail|
newID = itemDetail[1].to_s.delete(""").strip
end

I have another declared array in my code, with many more ID values like
those shown above

moreIDs = [“00000001”, “00000003”, “00000004”, “00000005”, 00000007",
“00000008”]

What I want to is search for all newID’s that match moreIDs and output
the matching ID and the corresponding code shown in column one. So the
sample output would be like this:

“2efa4ba470”, "00000005"“00000004”
“c0784b5de101”, “00000003”
“c0784b5de101”, “00000001”

Show this shows the string code for an ID that is present in both of my
lists. I had thought something like moreID == newID but that creates a
lot off do loops. Also, thr though of array intersects crossed my mind,
but we have a hash and array hear so I was unsure of how to make this
work?

Any assistance in greatly appreciated.

Ne Scripter wrote:

Ah yes, that makes perfect sense.

Thanks

Paul S. wrote:

You want a hash where the key is the element you want to group on, and
the ‘item’ is an array of all items with the shared key. A bit like
(untested):

hashMapping = {}

IO.foreach(source.to_s) do |data|
fields = data.split(",")
hash = fields[0]
ocrID = fields[1]

hashMapping[ocrID] ||= [] #If hashMapping has never seen this key
before, make an empty array

hashMapping[ocrID] << hash #Add the new element to the array for this
key

end

Many thanks

Posted via http://www.ruby-forum.com/.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

fields = data.split(",")
hash = fields[0]
ocrID = fields[1]

BTW, you could write this as:

hash, ocrID = *data.split(",")

or

hash, ocrID, *ignored = *data.split(",")

if you want to ignore everything behind a second comma, or

hash, ocrID = *data.split(",", 2)

if you want to include a second comma and everything behind it into the
ocrID string.

mfg, simon … which color is the green bill? blue!

Ah yes, that makes perfect sense.

Thanks

Paul S. wrote:

You want a hash where the key is the element you want to group on, and
the ‘item’ is an array of all items with the shared key. A bit like
(untested):

hashMapping = {}

IO.foreach(source.to_s) do |data|
fields = data.split(",")
hash = fields[0]
ocrID = fields[1]

hashMapping[ocrID] ||= [] #If hashMapping has never seen this key
before, make an empty array

hashMapping[ocrID] << hash #Add the new element to the array for this
key

end

Many thanks

Posted via http://www.ruby-forum.com/.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

I am having real problems with my array. As expected my array contents
is like so:

“00000004 00000005”
“00000003 00000006”
“00000001”
“00000002”

I now want 6 indivdual elements insteal of 4. I have tried to split the
double entries up with split, however they remain together. Is there
something fundamental I am missing? I want an array like so

array = [“00000004”, “00000005”, “00000003”, “00000006”, “00000001”,
“00000002”]

Sorry if this is simple, I have spent several hours chopping and
changing.

Thanks

S

David A. Black wrote:

On Thu, 1 Oct 2009, Simon K. wrote:

or

hash, ocrID, *ignored = *data.split(",")

if you want to ignore everything behind a second comma, or

hash, ocrID = *data.split(",", 2)

if you want to include a second comma and everything behind it into the
ocrID string.

I don’t think you need the * for any of them. This:

hash, ocrID = data.split(’,’)

should be fine for getting the first two values.

David

On Thu, 1 Oct 2009, Simon K. wrote:

or

hash, ocrID, *ignored = *data.split(",")

if you want to ignore everything behind a second comma, or

hash, ocrID = *data.split(",", 2)

if you want to include a second comma and everything behind it into the
ocrID string.

I don’t think you need the * for any of them. This:

hash, ocrID = data.split(’,’)

should be fine for getting the first two values.

David

On Thu, Oct 1, 2009 at 8:15 AM, Ne Scripter
<[email protected]

wrote:

something fundamental I am missing? I want an array like so

David


Posted via http://www.ruby-forum.com/.

x = [
“00000004 00000005” ,
“00000003 00000006” ,
“00000001” ,
“00000002” ,
]

x.map!{|str| str.split(/\s+/) }.flatten!

x # => [“00000004”, “00000005”, “00000003”, “00000006”, “00000001”,
“00000002”]

Hi –

On Thu, 1 Oct 2009, Josh C. wrote:

“00000002”]
You can even dispense with the argument to split if it’s just
whitespace.

Another option, though perhaps a slightly memory-wasting one:

x.join(’ ').split

David

hash, ocrID = *data.split(",")
ocrID string.

I don’t think you need the * for any of them.

I always include it anyway, to be explicit.

This:

hash, ocrID = data.split(’,’)

should be fine for getting the first two values.

Right, I just tried it. I thought you might be getting an array into
ocrID, but you don’t.

mfg, simon … l

Ah map. Perfect.

Josh C. wrote:

On Thu, Oct 1, 2009 at 8:15 AM, Ne Scripter
<[email protected]

wrote:

something fundamental I am missing? I want an array like so

David


Posted via http://www.ruby-forum.com/.

x.map!{|str| str.split(/\s+/) }.flatten!

x # => [“00000004”, “00000005”, “00000003”, “00000006”, “00000001”,
“00000002”]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs