2009/2/16 Valentino Lun <sumwo / yahoo.com>:
> Dear all
>
> I have an array with size around 1000, I want to perform some data
> checking and correction in this array.
>
> For instance, the first record of this array is a hash, as follow
> my_array[0] = {"server"=>"AHN", "hosp"=>"AHN", "loc"=>"PC1",
> "pspec"=>"ANA", "number"=>"1", "pcat"=>"1"}
>
> server  hosp  loc   pspec   pcat
> AHN     AHN   PC1   ANA     1
> PWH     AHN   PC1   ANA     1
> NDH     AHN   PC1   ANA     2     <= This pcat value need update in
> array1
> TMH     AHN   PC1   ANA     2     <= This pcat value need update in
> array1
> .......
> .....
> ...
> (around 1000 records)
>
> When keys hosp, loc, pspec has the same values, their pcat must be
> identical. So, there is problem in the last two records, the key pcat
> should be 1, because the pcat is correct if array["server"] equal to
> array["hosp"].
>
> I cannot figure out the logic to doing this in ruby (even in other
> language). Can someone give me some hints on this? Thanks

IMHO this is plainly the wrong data structure for the task.  Since you
identify entries by their hosp, loc, pspec you should *index* the
whole thing by these columns.  Also, since your Hashes seem to be
uniform I would rather define a particular type for this, e.g.

Entry = Struct.new :server, :hosp, :loc, :pspec, :pcat

EntryKey = Struct.new :server, :hosp, :loc do
  def self.create(entry)
    new(*members.map {|m| entry[m]})
  end
end

index = Hash.new {|h,k| h[k] = []}
# loop reading input
  entry = ...
  index[EntryKey.create(entry)] << entry

# now you can process them or do it while reading

See also Martin's reply which goes into the same direction just with a
different approach.

Cheers

robert


-- 
remember.guy do |as, often| as.you_can - without end