473,543 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

padding left-justified string fields

I've written a perl program to manipulate data formats so that I can import
data from an estimating software program to an accounting program. Works
just fine, with the following caveat: The record identifier field is
numeric in the estimating program but it can be (and is interpreted as) a
string in the accounting software. Hence, when I bring up project data in
the accounting software, my items look like so:

10
100
110
....
190
20
210
220

These fields MUST be left-justified for importation into the account s/w, so
I am using "@<<<<<<" in my format block. I'd like to be able to pad all
items to four decimal places with zeroes so that I'd end up with:

010
020
030
....
090
100
etc.

Can anyone tell me of a simple way to do this in perl? I've read and read
and read the manpages on printf() and sprintf() but can't seem to figure out
the syntax. Basically I'm printing out 19 fields, the first three are
left-justified (strings) with varying lengths and the rest for the most part
are seven-dot-three numerics. It would have been easier I'm sure to use
printf with some modifiers rather than type in all those "###'s" and
"<<<'s". Can anyone point me to an online resource to figure out printf
with some real world examples?

Thanks

Dave
Jul 19 '05 #1
7 22249
Dave wrote:
I'd like to be able to pad all items to 3 decimal places with zeroes
Can anyone tell me of a simple way to do this in perl?


It's simple.

printf "%03d %4d %s\n", $number1, $number2, $string;

That will make $number1 be padded to three places with zeros
and $number2 padded to four places with spaces, followed
by a string and ending with a newline.
-Joe

Jul 19 '05 #2

"Joe Smith" <Jo*******@inwa p.com> wrote in message
news:t8HJc.6590 1$WX.45392@attb i_s51...
Dave wrote:
I'd like to be able to pad all items to 3 decimal places with zeroes
Can anyone tell me of a simple way to do this in perl?
It's simple.

printf "%03d %4d %s\n", $number1, $number2, $string;

Thanks Joe. Can you tell me how to specify left vs. right-justified with
printf? Also, if I have a 7-dot-3 decimal format, is it possible to specify
the location of the decimal point?

thx

Dave
That will make $number1 be padded to three places with zeros
and $number2 padded to four places with spaces, followed
by a string and ending with a newline.
-Joe

Jul 19 '05 #3
Dave wrote:
[...] Can you tell me how to specify left vs. right-justified
with printf? Also, if I have a 7-dot-3 decimal format, is it
possible to specify the location of the decimal point?


Did you check "perldoc -f sprintf" as suggested in "perldoc -f printf"?

jue
Jul 19 '05 #4

"Joe Smith" <Jo*******@inwa p.com> wrote in message
news:t8HJc.6590 1$WX.45392@attb i_s51...
Dave wrote:
I'd like to be able to pad all items to 3 decimal places with zeroes
Can anyone tell me of a simple way to do this in perl?


It's simple.

printf "%03d %4d %s\n", $number1, $number2, $string;

That will make $number1 be padded to three places with zeros
and $number2 padded to four places with spaces, followed
by a string and ending with a newline.


Okay, that's all good. But my predicament is that I want something like
"10" padded to "010" AND I want it left-justified... the manpage for printf
states "If the 0 and - flags both appear, the 0 flag is ignored." Also, my
input format requires fixed-width fields; hence if I've got "10" and I
choose pad (i.e. printf "%010d") I end up with 0000000010 which I definitely
don't want imported as my string.

I guess maybe I could use printf to pad my values, then format to output
properly re: left-justification, etc.
Jul 19 '05 #5

"Jürgen Exner" <ju******@hotma il.com> wrote in message
news:YO******** **********@nwrd dc03.gnilink.ne t...
Dave wrote:
[...] Can you tell me how to specify left vs. right-justified
with printf? Also, if I have a 7-dot-3 decimal format, is it
possible to specify the location of the decimal point?


Did you check "perldoc -f sprintf" as suggested in "perldoc -f printf"?

jue

I did. I swear it. I tried many combinations and permutations. I ended up
using a two-step approach: I used printf to pad my values as part of my
array assignment loop, then format/write for justification and field
placement. I could use printf twice, but already had the output format all
set up for write. This approach ended up being the simplest for me, not
being skilled in the ways of printf.

I can see that in the big picture, printf is much more economical (saves
many many keystrokes) and I will probably put some time into broadening my
understanding of it.
Jul 19 '05 #6
Dave wrote:
"Jürgen Exner" <ju******@hotma il.com> wrote in message
news:YO******** **********@nwrd dc03.gnilink.ne t...
Dave wrote:
[...] Can you tell me how to specify left vs. right-justified
with printf? Also, if I have a 7-dot-3 decimal format, is it
possible to specify the location of the decimal point?


Did you check "perldoc -f sprintf" as suggested in "perldoc -f printf"?

jue


I did. I swear it.


You didn't read all of it.

You've missed the line with "left-justify within the field".
Go back and read it again.
-Joe
Jul 19 '05 #7
Dave wrote:
Okay, that's all good. But my predicament is that I want something like
"10" padded to "010" AND I want it left-justified... the manpage for printf
states "If the 0 and - flags both appear, the 0 flag is ignored."


It appears you did not recognize the significance of this line:

.number "precision" : digits after decimal point for
floating-point, max length for string, minimum length
for integer

That line explains what you should expect from
printf "%10.3\n",9 9;

-Joe
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5623
by: delerious | last post by:
Could someone please take a look at this page: http://home.comcast.net/~delerious1/index11.html The set of links on the left should not have any whitespace between them, and the set of links on the right should have one pixel of whitespace between them. That's how it looks in IE and Opera. But in Mozilla, a few of the links on the left...
0
1827
by: Red | last post by:
This is apparently an ie display bug, I can't seem to figure out which ie bug this is. a 3 sided border is created by wrapping the 'inner' box in the 'middle' box and padding the 'middle' box 1px on 3 sides. It looks the same in firefox and ie. Then the same box is wrapped in yet another box with a red border on the bottom. In Firefox...
7
8818
by: Gustaf Liljegren | last post by:
I continued on the example shown earlier today: http://gusgus.cn/test/index.html Now I get some unwanted space in Firefox (the red space just below the first image) which doesn't appear in IE6. I've double-checked the CSS for any padding or margings than may cause it, but have found none. How can I remove it? Thanks,
4
4273
by: Wilhelm Kutting | last post by:
hi, when i use the padding-left attribut, i like to overwrite a default value like that ..padding30 {margin-left: 30px;} ..padding0 {margin-left: 0px;} <div class="padding30"> Padding 30 <div id="padding0">
2
4396
by: Remi Villatel | last post by:
Hi there, I have following CSS definitions: div.limits { margin: 0 20px 0 20px; } div.halfleft { float: left; left: 0; width: 50%;
36
2986
by: phil-news-nospam | last post by:
Here is a simpler (no drop shadows) example of the padding bug I see: http://phil.ipal.org/usenet/ciwas/2006-05-08/buttons-1.html So far I find nothing in the CSS2 document that says I should get this kind of inconsistent result. -- ----------------------------------------------------------------------------- | Phil Howard KA9WGN ...
10
12882
by: Alan Silver | last post by:
Hello, In my (seemingly) endless quest to understand CSS, I have yet another problem. Please look at http://www.kidsinaction.org.uk/ph/x.html in Opera, where you will see it how I expected. If you look at it in IE (6 or 7), you find it also looks fine, except for the top margin missing from the links across the top of the page. That's not...
11
6846
by: sllrphoto | last post by:
A veteran of early html, I've modified my blog template and made it look rather clean (albeit rather plain) when viewed with IE. Ironically, I'm a big Firefox fan, but when viewed in FF, my blog leaves a lot to be desired. The blog (which is at http://lazycomic.blospot.com/ ) is a basic two column style, with the right column being the...
3
3872
by: xoinki | last post by:
hi all, I have a DIV element for which a table is appended.. CSS for this div element is.. .optiondiv { display:block; padding: 0px 30px; padding-left: 30px; padding-right: 30px;
6
4703
by: maya | last post by:
hi, I recently discovered the hard way that when you had padding-right or padding-left to a div it increases the with of the div... how do you add left-padding or right-padding to a div w/o changing the width of it? (for example: I have a div that's 320px wide, content inside is 300px and I added padding-left:10px but it increased with...
0
7341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7584
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7677
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5258
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3385
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
952
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.