I was wondering if there is an easy/clean way of swapping values as follows, perhaps using a single regex/substitution?
If $a ends with "x", substitute it with "y". And similarly if $a ends with "y", swap it with "x":
$a = "test_x";
if ($a =~ /x$/) {
$a =~ s/x$/y/;
} else {
$a =~ s/y$/x/;
}
I can only think of something like this:
$a = $a =~ /x$/ ? s/x$/y/ : s/y$/x/;
This is simply:
$a =~ s/x$/y/ or $a =~ s/y$/x/;
It's almost always redundant to do a match to see if you should do a substitution.
Another way:
substr($a,-1) =~ y/xy/yx/;
You can squeeze it in a line like you show, perhaps a bit nicer with /r
(with v5.14+).
Or you can prepare a hash. This also relieves the code from hard-coding particular characters.
my %swap = (x => 'y', y => 'x', a => 'b', b => 'a'); # expand as needed
my @test = map { 'test_' . $_ } qw(x y a b Z);
for my $string (@test)
{
$string =~ s| (.)$ | $swap{$1} // $1 |ex;
say $string;
}
The //
(defined-or) is there to handle the case where the last character isn't in the hash, in which case $swap{$1}
returns undef
. Thanks to user52889 for the comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With