Here is one approach.
#!/usr/bin/perl -w use strict; my @leftCon = qw(left123 left456); my @rightCon = qw(right123 right456 right789); my %l_con; my %r_con; @l_con{@leftCon} = (1 .. @leftCon); @r_con{@rightCon} = (1 .. @rightCon); while (<DATA>) { next unless /:/; chomp; my ($ping1, $ping2) = split /:/; $ping1 =~ s/(?<=\d\d\d)\d+$//; $ping2 =~ s/(?<=\d\d\d)\d+$//; s/$ping1/$l_con{$ping1}."L"/e if defined $l_con{$ping1}; s/$ping2/$l_con{$ping2}."L"/e if defined $l_con{$ping2}; s/$ping1/$r_con{$ping1}."R"/e if defined $r_con{$ping1}; s/$ping2/$r_con{$ping2}."R"/e if defined $r_con{$ping2}; print $_," "; } print "\n"; __DATA__ left1232:right4562 right1234:right4563 right7895:left4565 right7891:left1232
Prints:
1L2:2R2 1R4:2R3 3R5:2L5 3R1:1L2

One aspect of the problem that wasn't well defined was whether left12345 can be interpreted as both left123 45 and left1234 5. I took the statement above that the connection names would contain 3 digits as the defining case.

Also, there is some duplicated code here that could probably be simplified upon further clarification.

--
John.


In reply to Re: string manipulation with arrays by jmcnamara
in thread string manipulation with arrays by Anonymous Monk

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.