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!


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.