开发者

perl sequence number generator

开发者 https://www.devze.com 2023-03-05 18:40 出处:网络
In bash, sequence numbers e.g. 222R5555 echo {0..9}{0..9}{0..9}{A..Z}{0..9}{0..9}{0..9}{0..9}> seqLis开发者_Python百科t.txt

In bash, sequence numbers e.g. 222R5555

echo {0..9}{0..9}{0..9}{A..Z}{0..9}{0..9}{0..9}{0..9}  > seqLis开发者_Python百科t.txt

Can that line be done shorter (less code) in perl? Is there a way to use the repeat operator on ranges in perl?

Thanks


With less code? No. Perl's string increment doesn't allow digits to precede letters, so you'd have to break it up into two ranges: '000' .. '999' and 'A0000' .. 'Z9999' and concatenate the values. That's certainly going to take more than 68 characters of code.


my $g0to9 = '{'.join(',', '0'..'9').'}';
my $gAtoZ = '{'.join(',', 'A'..'Z').'}';
my %glob = join('', $g0to9 x 3, $gAtoZ, $g0to9 x 4);
while (my $_ = glob($glob)) {
   ...
}

or

[ Deleted ]

or

for my $p1 ('000'..'999') {
   for my $p2 ('A0000'..'Z9999') {
      my $_ = "$p1$p2";
      ...
   }
}

or

for my $ch0 ('0'..'9') {
for my $ch1 ('0'..'9') {
for my $ch2 ('0'..'9') {
for my $ch3 ('A'..'Z') {
for my $ch4 ('0'..'9') {
for my $ch5 ('0'..'9') {
for my $ch6 ('0'..'9') {
for my $ch7 ('0'..'9') {
   my $_ = join '', $ch0, $ch1, $ch2, $ch3, $ch4, $ch5, $ch6, $ch7;
   ...
}}}}}}}}

or

use Algorithm::Loops qw( NestedLoops );
my $i = NestedLoops([
   (['0'..'9'])x3,
   (['A'..'Z']),
   (['0'..'9'])x4,
]);
while (my @chs = $i->()) {
   my $_ = join '', @chs;
   ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消