1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
my @new_sn = get_new_numbers(1000, "10011902APWPA"); foreach(@new_sn){ print $_."\n"; } # A = 65 - Z = 90 sub get_new_numbers{ my $anzahl = shift; my $last_sn = shift; my @back_array; #print $last_sn."\n"; my ($mat, $last5); if( $last_sn =~ /(\w*)(\w{5})/ ) { $mat = $1; $last5 = $2; } my @c = split(//,$last5); foreach(@c){ $_ = ord($_); } foreach(1..$anzahl){ @c = reverse(@c); my $help_var = 0; foreach(@c){ ($help_var, $_) = count($_); last unless $help_var; } @c = reverse(@c); my $new_sn = $mat; foreach(@c){ $new_sn .= chr($_); } push(@back_array, $new_sn); } return @back_array; } sub count{ my $c_dec = shift; if( ++$c_dec > 90 ){ $c_dec = 65; return (1, $c_dec); } return (0, $c_dec); }
1 2 3 4 5 6 7 8 9
sub get_new_numbers { my ($num, $string) = @_; my ($mat, $last5) = $string =~ m/^(\w*)(\w{5})$/ or die "doesn't match"; my @numbers; for (1 .. $num) { push @numbers, $mat . ++$last5; # automagie } return @numbers; }
push @numbers, $mat . --$last5; # automagie
QuoteThe auto‐increment operator has a little extra builtin magic to it. If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. If, however, the variable has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern "/^[a−zA−Z]*[0−9]*\z/", the increment is done as a string, preserving each character within its range, with carry:
print ++($foo = "99"); # prints "100"
print ++($foo = "a0"); # prints "a1"
print ++($foo = "Az"); # prints "Ba"
print ++($foo = "zz"); # prints "aaa"
"undef" is always treated as numeric, and in particular is changed to 0 before incrementing (so that a post‐increment of an undef value will return 0 rather than "undef").
The auto‐decrement operator is not magical.
2014-01-27T13:27:54 Gustlich möchte aus z.B. folgender Seriennummer 10011902APWPA die darauf folgenden 1000 Seriennumern ausgeben. Wobei die Zahl immer gleich bleibt und er die letzten 5 Chars zählen soll. Von A bis Z. Wenn dann der letzten Stelle ein Z steht und hochgezählt werden muss springt dieses auf A und die nächste Stelle wird hochgezählt.
Beispiel:
10011902APYAW
10011902APYAX
10011902APYAY
10011902APYAZ
10011902APYBA
10011902APYBB
1 2 3 4 5 6 7 8 9 10 11 12 13
sub get_new_numbers{ my $anzahl = shift; my $last_sn = shift; # Seriennummer zerlegen my ($num,$str) = split(/(<=\d)(?=[A-Z])/,$last_sn); # Seriennummer inkrementieren und in Array schreiben my @back_array; push @back_array, $num.(++$str) foreach(1..$anzahl); return @back_array; }