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?
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):
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):
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[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
It is slightly more efficient to do it in one step:
(hashMapping[ocrID] ||= ) << hash
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.
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.
···
On Tue, Sep 29, 2009 at 2:38 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
2009/9/29 Paul Smith <paul@pollyandpaul.co.uk>:
On Tue, Sep 29, 2009 at 12:43 PM, Ne Scripter >> <stuart.clarke@northumbria.ac.uk> wrote:
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:
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 Smith 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):
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:
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
It is slightly more efficient to do it in one step:
(hashMapping[ocrID] ||= ) << hash
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.
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.
Cheers
robert
···
2009/9/29 Paul Smith <paul@pollyandpaul.co.uk>:
On Tue, Sep 29, 2009 at 2:38 PM, Robert Klemme > <shortcutter@googlemail.com> wrote:
2009/9/29 Paul Smith <paul@pollyandpaul.co.uk>:
On Tue, Sep 29, 2009 at 12:43 PM, Ne Scripter >>> <stuart.clarke@northumbria.ac.uk> wrote:
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
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
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 Krahnke 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
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