Gossamer Forum
Home : General : Perl Programming :

printf - need help with attribute

Quote Reply
printf - need help with attribute
I use the following with no problems

$bob = "a string";

printf("%-15s",$bob);

to print the following (padded to 15 characters):

"a string_ _ _ _ _ _ _"

However, I'm having trouble locating the attribute that would truncate the string. I have a field that I simply want to print on a page that is *limited* to x number of characters and was trying to use printf.

Example:

$bob = "a really long string and I only want part of it";

would return:

"a really long stri";
Quote Reply
Re: [Watts] printf - need help with attribute In reply to
you want substr for that.

say you have
Code:
$bob = "a really long string and I only want part of it";

and you only want the first 15 characters, use:

Code:
$bob = substr($bob, 0, 15);

you could also do:

Code:
my ($foo) = $bob =~ /^(.{0,15})/;

but you really shouldn't do that unless you have a more complicated expression to extract...

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] printf - need help with attribute In reply to
D'oh! I knew that!

Many thanks!

Last edited by:

Watts: Aug 23, 2006, 8:26 AM
Quote Reply
Re: [Watts] printf - need help with attribute In reply to
In Reply To:

However, I'm having trouble locating the attribute that would truncate the string. I have a field that I simply want to print on a page that is *limited* to x number of characters and was trying to use printf.

Example:

$bob = "a really long string and I only want part of it";

would return:

"a really long stri";



Since you say you need help with printf, you can do the same thing using s?printf (for more possibilities with s?printf, see perldoc -f sprintf)
$bob = sprintf ('%.10s', 'a really long string and I only want part of it');
# $bob eq 'a really l'

Last edited by:

mkp: Aug 26, 2006, 7:09 PM
Quote Reply
Re: [mkp] printf - need help with attribute In reply to
I'll check out the link. Thanks. I've used sprintf to "pad" fields (with spaces or zeros) but didn't have the code to "limit" the field. Between Perl, JavaScript & PHP - none of which I use everyday - it's hard to remember what does what. Crazy
Quote Reply
Re: [Watts] printf - need help with attribute In reply to
It doesn't help that the perlfunc man page isn't very clear on what all the (s)printf formats actually do, either.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] printf - need help with attribute In reply to
Quote:
precision, or maximum width
You can specify a precision (for numeric conversions) or a
maximum width (for string conversions) by specifying a "."
followed by a number.
...
For string conversions, specifying a precision truncates
the string to fit in the specified width:

printf ’<%.5s>’, "truncated"; # prints "<trunc>"
printf ’<%10.5s>’, "truncated"; # prints "< trunc>"
Quote Reply
Re: [mkp] printf - need help with attribute In reply to
Wow. That's more than perl.com had listed...

NAME
printf - output a formatted list to a filehandle

------------------------------------------------------------
SYNOPSIS
printf FILEHANDLE FORMAT, LIST

printf FORMAT, LIST

------------------------------------------------------------
DESCRIPTION
Equivalent to print FILEHANDLE sprintf(FORMAT, LIST), except that $\ (the output record separator) is not appended. The first argument of the list will be interpreted as the printf() format. If use locale is in effect, the character used for the decimal point in formatted real numbers is affected by the LC_NUMERIC locale. See the perllocale manpage.

Don't fall into the trap of using a printf() when a simple print() would do. The print() is more efficient and less error prone.
------------------------------------------------------------

>> That's like looking something up in "help" and it just gives you a description instead of a solution.

"The Buffer Override Error is an error that occurs when the buffer is overridden."

Last edited by:

Watts: Aug 31, 2006, 10:22 AM
Quote Reply
Re: [Watts] printf - need help with attribute In reply to
the explainations are actually listed under sprintf, to avoid duplication since printf is more or less identical.

Philip
------------------
Limecat is not pleased.