How can I parse this syntax into an array in Perl?

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 ($_ =~ /-/) { # parse the range
            < how do I parse the lower and upper limits? >
            for($lower..$upper) {
                $arr[++$#arr] = $_;
            }
        } else { # parse the first value
            < how do I parse the first value? >
        }

        # parse the rest of the values after the comma
        < how do I parse the values after the comma? >
    }
}
  • . , , ( -, , ). - ( , ?)?

  • / .

+3
7

, $_ , . chomp.

while (<$fh)>
{
    chomp (my $line = $_);
    # ...
}

"RANGE" , . , :

next if $line !~ /^RANGE (.*)$/;

, :

my @ranges = split /, /, $1;

. - , ..; :

@ranges = map { /(\d+)-(\d+)/ ? ($1 .. $2) : $_ } @ranges;

, :

my @numbers;
while (<$fh)>
{
    chomp (my $line = $_);
    next if $line !~ /^RANGE (.*)$/;

    push @numbers, map { /(\d+)-(\d+)/ ? ($1 .. $2) : $_ } (split /, /, $1);
}
+4

Text::NumericList CPAN. , :

use Text::NumericList;
my $list = Text::NumericList->new;

$list->set_string('1-3,5-7');
my @array = $list->get_array;     # Returns (1,2,3,5,6,7)

.

+5

?

, , , "-" :

if ($line =~ /RANGE ([\d\,\- ]+)/) {
    my $paramtxt = $1;
    my @elements = split(/\,/, $paramtxt);
    for my $element (@elements) {
        if ($element =~ /(\d+)\-(\d+)/) {
            $lower = $1;
            $upper = $2;
            push @arr, $lower .. $upper;
        } elsif ($element =~ /(\d+)/) {
            $solo = $1;
            push @arr, $solo;
        }
    } 
}
+3

Perl || :

map {  my($x,$y)=split/-/; $x..$y||$x } split /\s*,\s*/;

-, split/-/ $x, $y $x $y map. $x $x .

+2

:

#! /usr/bin/perl

use warnings;
use strict;

use 5.10.0;

my @tests = (
  "RANGE 34",
  "RANGE 45, 234",
  "RANGE 2-99",
  "RANGE 3-7, 15, 16, 2, 54",
);

for (@tests) {
  my %hits;
  @hits{$1 .. $2 // $1} = ()
    while /(\d+)(?:-(\d+))?/g;

  my @array = sort { $a <=> $b } keys %hits;
  print "@array\n";
}

:

34
45 234
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
2 3 4 5 6 7 15 16 54
+1

, :

#!/usr/bin/perl

use strict; use warnings;

my $number = '[0-9]+';
my $range  = "$number(:?-$number)?";
my $ranges = "$range(:?, $range)*";
my $pattern = qr/^RANGE ($ranges)$/;


while ( my $range = <DATA> ) {
    next unless $range =~ $pattern;
    my $expanded = expand_ranges($1);
    print "@$expanded\n\n";
}

sub expand_ranges {
    my ($ranges) = @_;
    my @terms = split /, /, $ranges;
    my @expanded;

    for my $term ( @terms ) {
        my ($lo, $hi) = split /-/, $term;
        push @expanded, defined( $hi ) ? $lo .. $hi : $lo .. $lo;
    }

    return \@expanded;
}


__DATA__
RANGE 34
RANGE 45, 234
RANGE 2-99
RANGE 3-7, 15, 16, 2, 54

:

34

45 234

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3
1 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

3 4 5 6 7 15 16 2 54
+1
source

Here are my efforts:

sub parse_range {
    my $str = shift;
    return unless $str =~ /^RANGE /g;

    my @array;
    while ($str =~ / \G \s* ( \d+ ) ( - ( \d+ ) ) ? \s* (?: , | $ ) /gxc) {
        push @array, $2 ? $1 .. $3 : $1;
    }

    return $str =~ /\G$/ ? @array : ();

}

It returns an empty list if the string parameter does not match the main format you have outlined.

0
source

Source: https://habr.com/ru/post/1765878/


All Articles