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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to insert spaces?? 2

Status
Not open for further replies.

jay123454

Programmer
Jun 15, 2001
46
US
hi-

i have a very simple problem..i hope at least. i'd like to have my perl program automatically insert spaces based on the number of characters i read from a variable so that everything is justified without using tabs....
for EX:

$var = rabbit;
$num = length($rabbit);
$spaces = (20 - $num);
print <<STOP
$var\s{$spaces}$var
STOP

something like this...can anyone help??

thanks!
 
#!/usr/local/bin/perl
$var = rabbit;
for (0..(19 - length($var))) { $var = ' '.$var; }
print &quot;$var\n&quot;;


There is probably a more concise way to express that, but, it is too early in the morning for me to thing about.


HTH


keep the rudder amid ship and beware the odd typo
 
Here's a nice little left pad subroutine that I use:
Code:
#---------------------------------------------------------------------
# LPad
#---------------------------------------------------------------------
# Pads a string on the left end to a specified length with a specified
# character and returns the result.  Default pad char is space.
#---------------------------------------------------------------------

sub LPad {

local($str, $len, $chr) = @_;

$chr = &quot; &quot; unless (defined($chr));
	
return substr(($chr x $len) . $str, -1 * $len, $len);

} # LPad
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I've got a right-pad function too, if anyone needs it.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top