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 | |
by Amblikai (Scribe) on Nov 29, 2013 at 14:21 UTC | |
by Corion (Patriarch) on Nov 29, 2013 at 14:25 UTC | |
Re: Comparing parts of 2 strings in an array and deleting one
by hdb (Monsignor) on Nov 29, 2013 at 14:22 UTC | |
by Amblikai (Scribe) on Nov 29, 2013 at 16:12 UTC | |
Re: Comparing parts of 2 strings in an array and deleting one
by wazat (Monk) on Nov 29, 2013 at 16:53 UTC |