This is a variation on choroba's solution. It's intended to illustrate how factoring regexes can make it easier to support more complex and changing requirements. (Note that the RX_RANGE_1 and RX_RANGE_2 regexes can be combined into a single regex and likewise RX_DATUM_1 and RX_DATUM_2, although arguably this makes them a bit more messy.) Some data validation is done as well. This code is minimally tested.

Win8 Strawberry 5.8.9.5 (32) Wed 10/20/2021 0:12:06 C:\@Work\Perl\monks >perl use warnings; use strict; use autodie; use Data::Dump 'dd'; my $data = <<'EOD'; [0..1] # ignore comment [9] # and blank lines [ 2 - 4 ] 87 11..14 EOD use constant RX_IGNORE => qr{ \A \s* (?: [#] [^\n]*)? \Z }xms; use constant RX_RANGE_1 => qr{ \A \s* \[ \s* (\d+) \s* (?: \.\. | -) \s* (\d+) \s* \] \s* \Z }xms; use constant RX_RANGE_2 => qr{ \A \s* (\d+) \s* (?: \.\. | -) \s* (\d+) \s* \Z }xms; use constant RX_DATUM_1 => qr{ \A \s* (\d+) \s* \Z }xms; use constant RX_DATUM_2 => qr{ \A \s* \[ \s* (\d+) \s* \] \s* \Z }xms; open my $fh, '<', \$data; my @permutations = []; LINE: while (my $line = <$fh>) { next LINE if $line =~ RX_IGNORE; # ignore blank/comment lines my @range = $line =~ RX_RANGE_1 ? ($1, $2) : # e.g. '[1..23]' or '[1-23]' $line =~ RX_RANGE_2 ? ($1, $2) : # e.g. '1..23' or '1-23' $line =~ RX_DATUM_1 ? ($1, $1) : # e.g. '23' $line =~ RX_DATUM_2 ? ($1, $1) : # e.g. '[23]' die "bad line '$line'" # default ; die "bad range: $range[0] > $range[1]: '$line'" if $range[0] > $range[1]; @permutations = map { my @prev = @$_; map [ @prev, $_ ], $range[0] .. $range[1]; } @permutations ; } # end while LINE close $fh; dd \@permutations; ^Z [ [0, 9, 2, 87, 11], [0, 9, 2, 87, 12], [0, 9, 2, 87, 13], [0, 9, 2, 87, 14], [0, 9, 3, 87, 11], [0, 9, 3, 87, 12], [0, 9, 3, 87, 13], [0, 9, 3, 87, 14], [0, 9, 4, 87, 11], [0, 9, 4, 87, 12], [0, 9, 4, 87, 13], [0, 9, 4, 87, 14], [1, 9, 2, 87, 11], [1, 9, 2, 87, 12], [1, 9, 2, 87, 13], [1, 9, 2, 87, 14], [1, 9, 3, 87, 11], [1, 9, 3, 87, 12], [1, 9, 3, 87, 13], [1, 9, 3, 87, 14], [1, 9, 4, 87, 11], [1, 9, 4, 87, 12], [1, 9, 4, 87, 13], [1, 9, 4, 87, 14], ]


Give a man a fish:  <%-{-{-{-<


In reply to Re: Assign multiple items into an array containing the range operator from a file by AnomalousMonk
in thread Assign multiple items into an array containing the range operator from a file by george59

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.