Find duplicates in each array rails

hi,

<% @sp_references.each do |sp_ref| %>
<% sp_ref.all_references.each_with_index do |tax_ref, i| %>
<%if (tax_ref.reference.uniq) && (tax_ref.reference !~ /emend$/i) %>
<%= tax_ref.reference %>
<%end%>
<%end%>
<%end%>

This ‘uniq’ option to get distinct elements in tax_ref.reference is not
working. It shows “undefined method `uniq’ for #<String:”. Kindly help
me
this regard to get the distinct elements from array using each in rails.


With Regards,
Palani

On Nov 29, 3:25pm, PalaniKannan K [email protected] wrote:

This ‘uniq’ option to get distinct elements in tax_ref.reference is not
working. It shows “undefined method `uniq’ for #<String:”. Kindly help me
this regard to get the distinct elements from array using each in rails.

Sounds like tax_ref.refrence is a string, not an array so calling uniq
on it doesn’t make any sense.

Fred

Dear Fred,

Yes I accept, any other way to extract the distinct content from each
array?


With Regards,
Palani Kannan. K

On 29 November 2010 16:23, PalaniKannan K [email protected]
wrote:

Yes I accept, any other way to extract the distinct content from each array?

Which array? The “sp_ref.all_references” array that you’re iterating,
or a different one?

Are you’re asking "how would I get a collection of distinct
“reference” values from each collection of “sp_ref.all_references”?

If so this might help…

<% @sp_references.each do |sp_ref| %>
<%= sp_ref.all_references.map(&:reference).uniq.delete_if{|ref| ref
=~ /emend$/i}.join(“
”) %>
<% end %>

… you could break it apart into separate operations if you wished.

Dear Michael,

Thanks lot for your answer.

<% @sp_references.each do |sp_ref| %>

<%= sp_ref.all_references.map(&:reference).uniq.delete_if{|ref| ref
=~ /emend$/i}.join("
") %>
<% end %>

I need the results have to print without duplicates. This gives the
results
with duplicates.

  • <% @sp_references.each do |sp_ref| %>
  • <%= sp_ref.all_references.map(&:reference).delete_if{|ref| ref=~
    /emend$/i}.join("
    ") %>
    <% end %>*

and

  • <% @sp_references.each do |sp_ref| %>
  • <%= sp_ref.all_references.map(&:reference).uniq.delete_if{|ref| ref=~
    /emend$/i}.join("
    ") %>
    <% end %>*

presents same results with duplicates. Please about your suggestion.

On 30 November 2010 14:10, PalaniKannan K [email protected]
wrote:

I need the results have to print without duplicates. This gives the results
with duplicates.

and

presents same results with duplicates. Please about your suggestion.

I’m having trouble seeing where you’re even trying to explain what you
have, and what you want…

Does this give what you’re after? :
<%=
@sp_references.map(&:all_references).flatten.map(&:reference).uniq.delete_if{|ref|
ref=~ /emend$/i}.join(“
”) %>

To be honest, I’m just stabbing in the dark, because you’ve not
explained anything about what these objects are made of or what actual
end result you want to achieve… some pseudo-code would help…