Luiz Vitor Martinez Cardoso
2/16/2009 11:13:00 AM
Using Symbols here make a big sense. Try to structure your array like:
my_array[0] =3D {:server =3D> "AHN", :hosp =3D>"AHN", :loc =3D>"PC1",
:pspec=3D>"ANA", :number=3D>"1", :pcat=3D>"1"}
And for all the values that are frequently repeated use Symbols. Basically
when you use Symbols you create one object and all the times that you use
one object with the same name you create a referece to this object and NOT
another object. Making that you will free memory.
Regards,
Luiz Vitor.
On Mon, Feb 16, 2009 at 7:57 AM, Martin DeMello <martindemello@gmail.com>wr=
ote:
> On Mon, Feb 16, 2009 at 3:30 PM, Valentino Lun <sumwo@yahoo.com> wrote:
> > 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] =3D {"server"=3D>"AHN", "hosp"=3D>"AHN", "loc"=3D>"PC1",
> > "pspec"=3D>"ANA", "number"=3D>"1", "pcat"=3D>"1"}
> >
> > server hosp loc pspec pcat
> > AHN AHN PC1 ANA 1
> > PWH AHN PC1 ANA 1
> > NDH AHN PC1 ANA 2 <=3D This pcat value need update in
> > array1
> > TMH AHN PC1 ANA 2 <=3D 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"].
>
> Simple way:
>
> 1. Have a 'signature' for each row, composed of the hosp, loc and
> pspec. Could be as simple as
>
> def signature(ary, row)
> %w(hosp loc pspec).map {|k| ary[row][k]}.join(",")
> end
>
> 2. Collect all the rows with the same signature
>
> verify =3D Hash.new {|h,k| h[k] =3D []}
> ary.each_with_index {|row, i|
> h[signature(ary, row)] << [i, row['pcat']]
> }
>
> 3. See if there are any problems
>
> verify.each_pair {|k, v|
> if v.length > 1
> fix_array_for(v)
> end
> }
>
> 4. Write fix_array_for(v)
>
> Note that v is an array of pairs of [index, pcat]. So for your
> example, it would be
> [[0,1], [1,1], [2,2], [3,2]]
>
> you basically need to iterate over that array, see which pcat is
> right, then iterate over it once more and set all the pcats to the
> right value.
>
> There are probably more efficient ways to do all this, but this has
> the advantage of being straightforward.
>
> martin
>
>
--=20
Regards,
Luiz Vitor Martinez Cardoso
cel.: (11) 8187-8662
blog: rubz.org
engineer student at maua.br
"Posso nunca chegar a ser o melhor engenheiro do mundo, mas tenha certeza d=
e
que eu vou lutar com todas as minhas for=C3=A7as para ser o melhor engenhei=
ro que
eu puder ser"