Thread '3,6-10,16,23' in (3,6,7,8,9,10,16,23) umwandeln (12 answers)
Opened by mixxer at 2019-08-22 22:28

haj
 2019-08-23 01:20
#190385 #190385
User since
2015-01-07
521 Artikel
BenutzerIn

user image
Ich würde hier Perl etwas "idiomatischer" schreiben: Wann immer man eine Liste in eine andere Liste umwandeln will, ist die Funktion Perldoc:perlfunc map eine passende Lösung.

Außerdem würde ich strengere Regeln an den Input anlegen, so dass Intervalle wie 'a-z' oder '-20-20' schlicht ignoriert werden. Dann sieht das so aus:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Data::Dumper;

my $text = '3,6-10,16,23';

my $list = &_text_to_list($text);
print Dumper $list;
exit;

sub _text_to_list {
    my @list = split( ',', shift );
    my @expanded = map {   /^(\d+)-(\d+)$/ ? ($1 .. $2)
                         : /^(\d+)$/       ? ($1)
                         : ()
                       } @list;
    my %hash = map { $_ => 1 } @expanded;
    @list = sort { $a <=> $b } keys %hash;
    return \@list;
}

View full thread '3,6-10,16,23' in (3,6,7,8,9,10,16,23) umwandeln