in reply to string manipulation with arrays


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.