I have a file containing parameters using this syntax
RANGE {<value> | <value>-<value>} [ , ...]
where valueare the numbers.
for example, all this is valid syntax
RANGE 34
RANGE 45, 234
RANGE 2-99
RANGE 3-7, 15, 16, 2, 54
How can I parse values in an array in Perl?
For example, for the last example, I want my array to have 3, 4, 5, 6, 7, 15, 16, 2, 54. The order of the elements does not matter.
The easiest way is to check the symbol -to determine if a range exists or not, analyze the range with a loop, and then analyze the rest of the elements
my @arr;
my $fh, "<", "file.txt" or die (...);
while (<$fh>) {
if ($_ =~ /RANGE/) {
if ($_ =~ /-/) {
< how do I parse the lower and upper limits? >
for($lower..$upper) {
$arr[++$#arr] = $_;
}
} else {
< how do I parse the first value? >
}
< how do I parse the values after the comma? >
}
}
. , , ( -, , ). - ( , ?)?
/ .