Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

Replies are listed 'Best First'.
Re^2: Comparing parts of 2 strings in an array and deleting one
by Amblikai (Scribe) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1064956]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-01-23 03:14 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (238 votes). Check out past polls.