There is a string as:
$string= 123456-9876;
Need to split it in array as follows:
$string = [12,34,56,98,76]
trying to split it as split('-',$string) is not serving the purpose. How could i do that in perl?
Rather than splitting, you can capture all 2 digit numbers with this perl code,
$str = "123456-9876";
my @matches = $str =~ /\d{2}/g;
print "@matches\n";
Prints,
12 34 56 98 76
Another solution, that just groups two digits no matter whatever, wherever non-digits are present in the string, without mutating the original string
$string = "1dd23-dsd--456-9-876";
while($string =~ /(\d).*?(\d)/g) {
print "$1$2 ";
}
Prints,
12 34 56 98 76
$string = [ $string =~ /(\d\d)/g ]([^-].)