#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11147549 use warnings; my $ranges = '5-10,14-18,22-27'; my $replace = 'B'; my $in = join '-', 1 .. 30; my $want = 'A-A-A-A-X-X-X-X-X-X-B-B-B-X-X-X-X-X-A-A-A-X-X-X-X-X-X-B-B-B'; my %X = map { $_ => 'X' } map { /-/ and $` .. $' } split /,/, $ranges; my $out = $in =~ s!\d+! $X{$&} // $& !ger =~ s/[-0-9]{2,}/ $replace ^= 'A'^'B'; $& =~ s|\d+|$replace|gr /ger; print "ranges $ranges\n out $out\n want $want\n"; $out eq $want or die "**Mismatch**";