Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
The stupid question is the question not asked
 
PerlMonks  

Re: Comparing parts of 2 strings in an array and deleting one

by Corion (Pope)
on Nov 29, 2013 at 14:16 UTC ( #1064956=note: print w/ replies, xml ) Need Help??


in reply to Comparing parts of 2 strings in an array and deleting one

Whenever you think "unique", think "hash".

perlfaq4 lists the common approach on how to find the unique items.

My approach to your problem would be to build a hash of

'extracted name' => 'original_line',

... and in the end, you only need to fetch the values of that hash.

If you use this approach, you lose the original order of your elements, so if that is important, you can use another approach that simply keeps track whether you've seen a particular name already:

my %seen; my @result; for my $el (@list1, @list2) { my $name= find_name_from_element($element); next if $seen{ $name }++; push @result, $el; }; # Now @result has the unique elements


Comment on Re: Comparing parts of 2 strings in an array and deleting one
Select or Download Code
Re^2: Comparing parts of 2 strings in an array and deleting one
by Amblikai (Sexton) on Nov 29, 2013 at 14:21 UTC

    Thanks for your reply!

    Maybe i was thinking along the right lines using a hash. However, your way is a lot more concise since i don't explicitly have to compare the values first before i use a hash. Thanks!

      I'm sorry - I completely missed that you are already using a hash. I did not read your code closely enough and thought you were using two loops. So you were quite close, and just missed the step from using the keys to remember what things you've seen to also using the values to remember the original format.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1064956]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-04-16 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (434 votes), past polls