2011-05-14 63 views
2

在bash中,序列號例如222R5555perl序列號發生器

echo {0..9}{0..9}{0..9}{A..Z}{0..9}{0..9}{0..9}{0..9} > seqList.txt 

該行可以在perl中更短(少代碼)嗎?有沒有辦法在perl中使用重複運算符?

謝謝

+4

想知道有多少人複製並粘貼這段代碼並運行它... – jm666 2011-05-14 23:16:43

+0

我希望任何人都可以使用perl – 2011-05-14 23:36:06

回答

6

使用較少的代碼?不。Perl的字符串增量不允許將數字放在字母之前,因此您必須將其分成兩個範圍:'000' .. '999''A0000' .. 'Z9999'並連接值。這肯定會佔用超過68個字符的代碼。

+0

將''A0000'..'Z9999''真的做對了嗎? – 2011-05-15 04:20:44

+0

@Ryan:是的。見[這個答案](http://stackoverflow.com/questions/3508362/autoincrementing-letters-in-perl/3508446#3508446)。 – cjm 2011-05-15 05:20:25

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

[ Deleted ] 

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

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; 
    ... 
}}}}}}}} 

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

''A000A0000'''A999Z9999''不起作用,因爲A000A0000與模式'/^[a-zA-Z] * [0-9] * \ z /'不匹配。請參閱[這個答案](http://stackoverflow.com/questions/3508362/autoincrementing-letters-in-perl/3508446#3508446)關於如何Perl的魔術字符串增量工作的詳細信息。 – cjm 2011-05-15 09:23:07

+0

謝謝,我使用@cjm建議像你的第三個片段。最後一個片段非常酷。它需要更新(['0'...'9'))x3,所以你需要重複操作符的括號。 – 2011-05-15 17:23:30

+0

@cjm,@Eric Fortis:謝謝,修正。我應該修復Perl,使嵌入的數字工作。 – ikegami 2011-05-15 20:29:36