0

I am trying to do string manipulation in perl like so.

/q1.pl asad566sads asad575sads

then this prints

asad566sads 
asad567sads
asad568sads
...
...
asad575sads

I am thinking somehow separate the string, then join it back up again?

#!/usr/bin/perl -w


if (@ARGV != 2){
    print "error\n";
}else{
    my $numb1 = $ARGV[0];
    my $numb2 = $ARGV[1];

    $numb1 =~ s/(\d*)\D/$1/g;
    $numb2 =~ s/(\d*)\D/$1/g;


    print "$numb1 \n";
    print "$numb2 \n";


    for ($i=$numb1; $i<$numb2; $i++){

        #my $numb_2_print;
    my $string_to_print = $ARGV[0];

    $string_to_print =~ s/(\D*)\d(\D*)/$1$i$2/g;
    print "$string_to_print\n";
    }
}

EDIT: assume numbers on appear once in string for this question. sorry about that

5
  • Is a1a1 a2a2 allowed? What should the result be? Commented Feb 26, 2014 at 21:37
  • for this question no. Can assume numbers only come once. Commented Feb 26, 2014 at 21:38
  • will they always have the same beginning and end? Commented Feb 26, 2014 at 21:41
  • yes the string around it will remain same. only the numbers change. Commented Feb 26, 2014 at 21:44
  • shrug Supported a1a1..a2a2 anyway Commented Feb 26, 2014 at 21:47

3 Answers 3

1
#!/usr/bin/perl -w

use strict;
use warnings;

die "Invalid number of parameters" if @ARGV != 2;

my ($pref1, $num1, $suf1) = $ARGV[0] =~ /^(\D*)(\d+)(\D*)$/ or die "Invalid Parameter";
my ($pref2, $num2, $suf2) = $ARGV[1] =~ /^(\D*)(\d+)(\D*)$/ or die "Invalid Parameter";

die "Prefixes don't match" if ($pref1 ne $pref2);
die "Suffixes don't match" if ($suf1 ne $suf2);

print "$pref1$_$suf1\n" for ($num1..$num2);

Addendum: If you care about the numbers being the same length, you can use this printf statement instead

printf "%s%0" . length($num2) . "d%s\n", $pref1, $_, $suf1 for ($num1..$num2);
Sign up to request clarification or add additional context in comments.

1 Comment

Added an alternative print statement in case you care about the numbers being 0 padded so they're the same length.
1
use Algorithm::Loops qw( NestedLoops );

my $s = 'a1a1';
my $e = 'a2a2';

my @s_parts = $s =~ /(\d+|\D+)/g;
my @e_parts = $e =~ /(\d+|\D+)/g;

die if @s_parts != @e_parts;

my @loops;
for my $i (0..$#s_parts) {
    if ($s_parts[$i] =~ /^\d/) {
       die if $s_parts[$i] > $e_parts[$i];
       push @loops, [ $s_parts[$i] .. $e_parts[$i] ];
    } else {
       die if $s_parts[$i] ne $e_parts[$i];
       push @loops, [ $s_parts[$i] ];
    }
}

NestedLoops(\@loops, sub {
   print(@_, "\n");
});

a1a1
a1a2
a2a1
a2a2

Comments

1

The range operator is your friend (http://perldoc.perl.org/perlop.html#Range-Operators):

#!/usr/bin/perl
use strict;
use warnings;

my ($start, $end) = @ARGV;
$start =~ s/([a-zA-Z]+)$//;
$end   =~ s/([a-zA-Z]+)$//;
my $trailing = $1;

foreach ( $start..$end ) {
   print "$_$trailing\n";
}

__END__
asad566sads
asad567sads
asad568sads
asad569sads
asad570sads
asad571sads
asad572sads
asad573sads
asad574sads
asad575sads

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.