Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Left and right padding of variable data

Left and right padding of variable data

Left and right padding of variable data

dears, pls help me

my input data:

what i should have:

I tried to resolve the problem with regular expressions, I doesn't succeeded! If somebody knows the trick pls let me know.

Thanks a lot and "grüezi"

RE: Left and right padding of variable data

perldoc -f sprintf

Beware: Studies have shown that research causes cancer in lab rats.

RE: Left and right padding of variable data

I ran into to this when writing reports of analytical results for an environmental testing company.  The results had to show the appropriate precision for the analytical test being reported.  I had to write a tedious but not complicated sub that padded the leading and trailing zeros.  NOTE that after you do the padding, the value is simply a string.  IF you do any math on the value, it drops the leading and trailing zeros and you are back where you started.  Sorry, I don't have the sub routine handy and don't have time, at the moment, to rewrite it..... maybe in a day or two.

Good Luck.  This is an annoying problem.

 keep the rudder amid ship and beware the odd typo

RE: Left and right padding of variable data

If you're writing to a printer (or other formatted output) then printf is the way to go. However, it you require a specific length variable inside the program for one reason or another (I know we do) what we have done is the:

while (length($var)<123) { $var="0".$var; }

Works just fine.  There might be other methods but if it's not broken...

RE: Left and right padding of variable data

Here's a handy pair of subroutines for padding strings. Call as:

$string = LPad($string, $length, $padchar);
$string = RPad($string, $length, $padchar);

sub LPad {
   my($str, $len, $chr) = @_;
   return substr(($chr x $len) . $str, -1 * $len, $len);

sub RPad {
   my($str, $len, $chr) = @_;
   return substr($str . ($chr x $len), 0, $len);

Hope these are useful. They're part of my standard libary.

RE: Left and right padding of variable data


In the meantime I did it my way, following my solution

Thanks anyway
#!perl -w

print "\n";

@temp = (                                                         # some testdata
for (@temp) {
 @extract = /([0-9]*)\.?([0-9]*)/g;                               # split up left and rigth side

  $pad_lhg = "10";                                                # padding left side     
   $paddet_lhg = sprintf("%0${pad_lhg}s", $extract[0]);           # padding left side cont.  

    if (@rh = ($extract[1]=~/([0-9]{2})/)) {                      # match auf zwei dingsda
         $paddet_rhg = $rh[0-1] ;                                 # no padding needet

    } elsif (@rh = ($extract[1]=~/([0-9]{1})/g)) {                # match auf ein dingsda
              $pad_char_rh = "0";                                 # padding right side with "0"
               $paddet_rhg = $rh[0].$pad_char_rh ;                # still padding

    } else {                                                      # wenn sonst was ist
             $paddet_rhg = "00" ;                                 # pad with "00"
 print "$paddet_lhg","$paddet_rhg","\n";

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close