http://www.perlmonks.org?node_id=1064954

Amblikai has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! I'm having a bit of a problem here where i'm sure there's an easier way of doing what i'm trying to do. I thought i would ask for some wisdom and maybe learn something in the process!

Basically i have 2 arrays which contain the data:

my @list_one=( 'parameter real name = 0.1', 'parameter real name1 = 0.2', 'parameter real name2 = 0.3' );
my @list_two=( 'parameter real name = 0.01', 'parameter real name2 = 2.2', 'parameter real name3 = 3.3' );

I want to compare the elements of the list only on "name" not on the value at the end. I want to keep the elements which are unique to both arrays. But if "name" appears in both lists, i only want the corresponding element from List one. Regardless of value.

Does that make sense?

Using splice to delete parts of the array is annoying cause then the index keeps changing!

At the moment i'm doing Something like this:

my $index=0; my %list_two_remaining=(); foreach my $list_two_element (@list_two) { $list_two_element=~m/.*parameter\s+real\s+(\w+).*/; my $list_two_name=$1; foreach my $list_one_element (@list_one) { $list_one_element=~m/.*parameter\s+real\s+(\w+).*/; my $list_two_name=$1; if ($list_one_element ne $list_two_element) { $list_two_remaining{$list_two_element}++; } } } my @list_two=keys(%list_two_remaining);

It's really basic and long winded! I'm sure there's a better way. Can you please help?

Thank you!

P.S I didn't run this code, it's hand typed. Sorry but i don't have my unix machine connected to this laptop. Long story!

Replies are listed 'Best First'.
Re: Comparing parts of 2 strings in an array and deleting one
by Corion (Patriarch) on Nov 29, 2013 at 14:16 UTC

    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

      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.

Re: Comparing parts of 2 strings in an array and deleting one
by hdb (Monsignor) on Nov 29, 2013 at 14:22 UTC

    Transforming your lists into a joined hash with list two first, so that list one will overwrite list two. Then transform back to list:

    my %joinedList = map { /(.*)=(.*)/ } @list_two, @list_one; my @list = map { "$_=$joinedList{$_}" } keys %joinedList;

    Disadvantage is that order is not preserved.

    Update: it can be done even easier like this:

    my @list = values %{{ +map { /(.*)=/; $1=>$_ } @list_two, @list_one }};

    By tayloring the part /(.*)=/; $1=>$_ you can transform your data as you like.

      I like this! Thanks!

Re: Comparing parts of 2 strings in an array and deleting one
by wazat (Monk) on Nov 29, 2013 at 16:53 UTC

    You could also try

    my %h1 = map { /^\w+\s+\w+\s+(\w+)/ ; ( $1, 1) } @list_one; push @list_one, grep { /^\w+\s+\w+\s+(\w+)/; ! exists $h1{$1} } @list_ +two;

    This retains the the ordering to some extent. I extracted the names a bit differently, but not necessarily in a better way.