Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

printf question

15 views
Skip to first unread message

John Black

unread,
May 6, 2013, 3:51:25 PM5/6/13
to
Hopefully a simple question...

Given this printf that specifies some fixed length left justified columns:

printf "%-30s, %-30s, %-30s\n", $a, $b, $c;

How can I make the hardcoded number 30 use a variable, say $len instead?

This does not work:

printf "%-$lens, %-$lens, %-lens\n", $a, $b, $c;

I'm guessing because perl would have trouble knowing that the s at the end is not part of the
variable? Thanks.

John Black

Peter J. Holzer

unread,
May 6, 2013, 4:08:20 PM5/6/13
to
Yes.

Either:

printf "%-${len}s, %-${len}s, %-${len}s\n", $a, $b, $c;

Or (more C-like):

printf "%-*s, %-*s, %-*s\n", $len, $a, $len, $b, $len, $c;

hp


--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
| | | h...@hjp.at | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpa�t. -- Ralph Babel

hymie!

unread,
May 6, 2013, 4:10:40 PM5/6/13
to
In our last episode, the evil Dr. Lacto had captured our hero,
John Black <jbl...@nospam.com>, who said:
>Hopefully a simple question...
>
>Given this printf that specifies some fixed length left justified columns:
>
> printf "%-30s, %-30s, %-30s\n", $a, $b, $c;
>
>How can I make the hardcoded number 30 use a variable, say $len instead?

$format = "%-${len}s, %-${len}s, %-${len}s\n";
printf $format, $a, $b, $c;

--hymie! http://lactose.homelinux.net/~hymie hy...@lactose.homelinux.net
-------------------------------------------------------------------------------

John Black

unread,
May 6, 2013, 6:06:23 PM5/6/13
to
In article <slrnkog3dk.sr...@hrunkner.hjp.at>, hjp-u...@hjp.at says...
>
> On 2013-05-06 19:51, John Black <jbl...@nospam.com> wrote:
> > Hopefully a simple question...
> >
> > Given this printf that specifies some fixed length left justified columns:
> >
> > printf "%-30s, %-30s, %-30s\n", $a, $b, $c;
> >
> > How can I make the hardcoded number 30 use a variable, say $len instead?
> >
> > This does not work:
> >
> > printf "%-$lens, %-$lens, %-lens\n", $a, $b, $c;
> >
> > I'm guessing because perl would have trouble knowing that the s at the
> > end is not part of the variable?
>
> Yes.
>
> Either:
>
> printf "%-${len}s, %-${len}s, %-${len}s\n", $a, $b, $c;
>
> Or (more C-like):
>
> printf "%-*s, %-*s, %-*s\n", $len, $a, $len, $b, $len, $c;
>
> hp

Thanks much. I guess with Perl, its probably going to be adding either {}, [] or ().

John Black

John Black

unread,
May 6, 2013, 6:07:24 PM5/6/13
to
In article <51880e40$0$36530$862e...@ngroups.net>, hy...@lactose.homelinux.net says...
>
> In our last episode, the evil Dr. Lacto had captured our hero,
> John Black <jbl...@nospam.com>, who said:
> >Hopefully a simple question...
> >
> >Given this printf that specifies some fixed length left justified columns:
> >
> > printf "%-30s, %-30s, %-30s\n", $a, $b, $c;
> >
> >How can I make the hardcoded number 30 use a variable, say $len instead?
>
> $format = "%-${len}s, %-${len}s, %-${len}s\n";
> printf $format, $a, $b, $c;

I like the idea of putting $format in its own variable for readability and for use in
multiple printf statements. Thanks.

John Black

Ben Morrow

unread,
May 6, 2013, 7:07:58 PM5/6/13
to

Quoth John Black <jbl...@nospam.com>:
my $format = sprintf "%%-%ds", $len;
$format = join ", ", ($format) x 3;
printf "$format\n", $a, $b, $c;

Ben

0 new messages