2

In bash, sequence numbers e.g. 222R5555

echo {0..9}{0..9}{0..9}{A..Z}{0..9}{0..9}{0..9}{0..9}  > seqList.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

Eric Fortis
  • 16,372
  • 6
  • 41
  • 62

2 Answers2

6

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.

cjm
  • 61,471
  • 9
  • 126
  • 175
  • @Ryan: Yes. See [this answer](http://stackoverflow.com/questions/3508362/autoincrementing-letters-in-perl/3508446#3508446). – cjm May 15 '11 at 05:20
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)) {
   ...
}

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;
   ...
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • `'A000A0000'..'A999Z9999'` does not work because A000A0000 does not match the pattern `/^[a-zA-Z]*[0-9]*\z/`. See [this answer](http://stackoverflow.com/questions/3508362/autoincrementing-letters-in-perl/3508446#3508446) for details on how Perl's magic string increment works. – cjm May 15 '11 at 09:23
  • thanks, I used @cjm advise like your 3rd snippet. The last snippet is very cool. it needs to be updated (['0'..'9'])x3, so you need parenthesis for the repeat operator. – Eric Fortis May 15 '11 at 17:23
  • @cjm, @Eric Fortis: Thanks, fixed. I should fix Perl so embedded digits work. – ikegami May 15 '11 at 20:29