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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found