Paul Nickerson wrote:
> In short, I'm looking to do this: integer 4 -> string dbt0004sfg, and
> integer 287 -> string dbt0287sfg.
>
> And now in long, I want to iterate through creating strings to print
> the bellow:
> dbt0001sfg
> dbt0002sfg
> ...
> dbt0034sfg
> ...
> dbt2601sfg
> ...
>
> I think what I'm looking for is a regular expression to take a string
> of some arbitrary length, and substitute it into a longer string at a
> constant, right justified position. I can calculate the left justified
> position if necessary, using integer division or grabbing the length
> of the first string. I'm familiar with several languages, and have
> been programming for some time now, but I'm still a beginner for Perl
> specifically. Below I have some skeleton code that I think will work
> for me, once I figure out the magic.
>
> for($i=1; $i<=9999; $i++)
>
> {
>
> $longer = "dbt0000sfg";
>
> $iChar = sprintf("%u", $i);
>
> # Magical regular expression stuff happens. #
>
> print "$longer\n";
>
> }
>
> So, anyone know if there's an elegant way to do this with regular
> expressions? I know I can do it with a bunch of if-then statements,
> but that's ugly, plus I think I may ultimately be doing this on 7 or
> more digits (I won't be changing how long each output needs to be
> midway through the program, don't worry).
>
> I've been going over regular expression intro's, and Googling for this
> specifically, but I haven't come across anything yet.
Oh dear. Someone posted my solution already. Better provide a different
one then!
foreach my $i (1 .. 9999) {
my $zfill = substr("0000$i", -4);
my $longer = "dbt${zfill}dsfg";
print $longer, "\n";
}
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/