Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > printf and variable length?

Thread Tools

printf and variable length?

Posts: n/a

I am using the below statement. It writes exactly 19 positions,
padding the right of the string with spaces if need be.

printf DB "%-19s",$value;

However, now I need to write $fieldlength positions. I cannot find how
I can combine printf with $fieldlength. Is this possible, or should I
first format my string and then print it? What is the most ecomical
way to do this?

Reply With Quote
Tad McClellan
Posts: n/a
Daemonice <(E-Mail Removed)> wrote:

> printf DB "%-19s",$value;
> However, now I need to write $fieldlength positions. I cannot find how
> I can combine printf with $fieldlength.

You can disambiguate the name of the variable with curly braces
(so that the "s" is not part of the variable name):

printf DB "%-${fieldlength}s", $value;

Tad McClellan SGML consulting Removed) Perl programming
Fort Worth, Texas
Reply With Quote
Posts: n/a
Thanks to everybody who answered, it was very useful for me!

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
Functions with variable amount of arguments i.e. printf() Jeff Rodriguez C Programming 1 12-07-2003 10:48 AM