Leser: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14
my $string = '1-3 5 8 9-10'; my @array; while ( $string =~ /(\d+(?:-\d+)?)/g ) { my ($start,$ende) = split /-/, $1; if ( $ende ) { push @array, $start .. $ende; } else { push @array, $start; } } print join "::", @array;
perl -pe 's/(\d+)-(\d+)/join " ", $1..$2/ge'
1 2 3 4 5 6 7 8
my @ranges = map { $_->[0] .. $_->[1] } map { my ($x, $y) = split /-/, $_; $y //= $x; # in perl < 5.10 muss es lauten: $y = $x unless defined $y; [$x,$y] } split / /, $_; print "Zahlen: @ranges\n";
1 2 3 4 5 6 7 8 9 10
use Parse::Range qw(parse_range); my $a=('1 3-6 9-13'); print $a,"\n"; my@b=split / /, $a; my$c=join ",",@b; #muss halt so aussehen: '1,3-6,8-13'; my @range=parse_range($c); print @range, "\n"; print $range[2];
1 2 3 4 5 6 7 8 9 10 11
use Parse::Range qw(parse_range); my $w='1 3-6 9-13'; print $w,"\n"; $w=~s/\s+/,/; print $w,"\n"; my @range=parse_range($w); print @range, "\n"; print $range[2];
1
2
3
my $eingabe = '1-3 5 8 9-10';
my @a = split(m/[-\s]/, $eingabe);
print join(', ', @a);