2009/4/2 George George <george.githinji / gmail.com>:

> i have a 3 arrays(A,B,C) which are results of some analysis.

I'd rather not make those constants (A,B,C) because they aren't as you stat=
ed.

> Array A has all members such that array B and C are actually subsets of
> A. The order of elements in array A is important.
>
> What i want is to list all members of A such that if an element in A is
> a found in B or C produce a report such as
>
> element1 -b
> element2 -b
> element3 -c
> element4 -b
> ....
>
> where the order of elements in the report is as was in array A.
>
>
> my rather silly implentation was
> A.each do |element|
> =A0if B.detect(element)
> =A0 =A0puts "#{element}- b"
> =A0end
> end
>
> however i don't think this is the right way to do it since it does not
> give me the designed result. Can someone help me spot the bug?

You want #include? instead of #detect:

a.each do |el|
  case
    when b.include? el
      puts "#{el} -b"
    when c.include? el
      puts "#{el} -c"
  end
end

Another hint: if A is really large (say 10_000 elements or more) it
might be more efficient to convert B and C into Sets which have faster
inclusion test.

Or do it this way:

occurrences =3D {}

{b =3D> "b", c =3D> "c"}.each do |ar,label|
  ar.each {|el| occurrences[el] =3D label}
end

a.each do |el|
  label =3D occurrences[el] and
    puts "#{el} -#{label}"
end

Kind regards

robert

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