#!/usr/bin/perl use 5.016; # 1030798ALT my $str = "chapter 1,2,3"; # out: "chapter 1,chapter 2,chapter 3" my ($chapter, @num); if ( $str =~ /^(chapter) / ) { $chapter = $1; # say "\$chapter: |$chapter|"; } my @chapnums = ( $str =~ /(\d[,]*)/gc); if ( @chapnums) { push @num,@chapnums; } for my $num(@num) { # say "DEBUG \$#num: $#num"; # if ( $1 != (1 + $#num) ) { # print "\$chapter $num" . ","; # } elsif ( $1 == (1 + $#num) ){ print "$chapter $num"; }