Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by Corion (Patriarch)
on Nov 29, 2013 at 14:16 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found