Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Left and right padding of variable data

  • Thread starter Thread starter hr
  • Start date Start date
Status
Not open for further replies.

hr

Technical User
Joined
Nov 11, 2000
Messages
29
Location
CH
dears, pls help me

my input data:
123456
123456.
123456.1
123456.15

what i should have:
010012345600
010012345600
010012345610
010012345615

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"

 
perldoc -f sprintf


Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
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
 
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=&quot;0&quot;.$var; }

Works just fine. There might be other methods but if it's not broken...
 
Here's a handy pair of subroutines for padding strings. Call as:
Code:
$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.
 
Dears

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

Thanks anyway
#!perl -w

print &quot;\n&quot;;

@temp = ( # some testdata
&quot;0001&quot;,
&quot;1012.&quot;,
&quot;2023.2&quot;,
&quot;3034.35&quot;,
&quot;4045.4&quot;,
&quot;5056.50&quot;,
&quot;6067.6&quot;,
&quot;7078.75&quot;,
&quot;8089.8&quot;,
&quot;9090&quot;,
);

for (@temp) {

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

$pad_lhg = &quot;10&quot;; # padding left side
$paddet_lhg = sprintf(&quot;%0${pad_lhg}s&quot;, $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 = &quot;0&quot;; # padding right side with &quot;0&quot;
$paddet_rhg = $rh[0].$pad_char_rh ; # still padding

} else { # wenn sonst was ist
$paddet_rhg = &quot;00&quot; ; # pad with &quot;00&quot;
}

print &quot;$paddet_lhg&quot;,&quot;$paddet_rhg&quot;,&quot;\n&quot;;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top