develooper Front page | perl.beginners | Postings from April 2008

Substitute Variably hort string into long string at Right Justified possition

Thread Next
From:
Paul Nickerson
Date:
April 17, 2008 03:57
Subject:
Substitute Variably hort string into long string at Right Justified possition
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.


Thread Next


Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About