in reply to Perl Module for dealing with number ranges

I certainly do not advocate doing it this way. Use one of the modules and reap the benefits of testing, a user base, docs you don't have to write, and future improvements. This is just for fun and because I found dmmiller2k's example a bit hard to read.

while (<DATA>) { chomp; print join(" + ", expand($_)), $/; } sub expand { my $input = shift; $input =~ s/[^-\d,]+//g; $input =~ s/-/../g; my ( %tmp, @tmp ); @tmp = ( eval "$input" ); @tmp{@tmp} = (1) x @tmp; # dedupe return sort { $a <=> $b } keys %tmp; } __DATA__ 1,7-10,3-5,15 or even ... my $input = "1,7-10,3-5,15";

Replies are listed 'Best First'.
Re^2:dealing with coordinate ranges
by Discipulus (Canon) on May 20, 2014 at 11:27 UTC
    mmh. some year is passed, but.. I need to expand coordinate ranges, comma separated like in 4 4 , 5-7 5, 0 0, 0-23 2

    With the help of moritz in the chat and plagiarizing rosetta code I ended with this:
    #!perl use strict; use warnings; my $input = "@ARGV"; my @pairs = split /,/, $input; foreach my $pair (@pairs){ $pair =~ s/^\s+//;$pair =~ s/\s+$//; print map {"->$_\n"} &exp_coord($pair); } sub exp_coord { my ($r,$c)=split /\s/,"@_"; unless (defined $r and defined $c) {warn "Both must be defined. Re +ceived:",map{defined $_ ? "$_ " : 'UNDEF '}($r,$c);return} my @r; my @c; my @expanded; @r = $r=~/^(.*\d)-(.+)$/ ? ($1..$2) : ($r); @c = $c=~/^(.*\d)-(.+)$/ ? ($1..$2) : ($c); for my $rc (@r) { for my $cc (@c) { push @expanded, "$rc $cc" } }; return @expanded; }
    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.