Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Assign multiple items into an array containing the range operator from a file

by AnomalousMonk (Bishop)
on Oct 20, 2021 at 06:16 UTC ( #11137773=note: print w/replies, xml ) Need Help??


in reply to Assign multiple items into an array containing the range operator from a file

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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11137773]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2022-01-18 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (52 votes). Check out past polls.

    Notices?