On Thu, Apr 17, 2008 at 4:14 AM, Paul Nickerson <[EMAIL PROTECTED]> 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.
snip

You are going to kick yourself when you see how easy this is in Perl:

#!/usr/bin/perl

use strict;
use warnings;

for my $id ('0000' .. '1000') {
        print "dbt${id}sfg\n"
}



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to