in reply to How to use sprintf %n formatting pattern
You could break the print into two parts so you can take advantage of knowing the prefix length using %n:
#!/usr/bin/env perl use 5.010; use warnings; use strict; my $max_width = 60; my $scale = '1.......10........20........30........40........50....... +.60'; my $long_string = q{The quick brown fox jumps over the lazy dog}; my $pat = "%.2f %d/%d/%d %n"; my @aoa = ( [ qw/79.3 2022 1 8 /], [ qw/394571 22 10 81 /], [ qw/123456.78 12345 123 1234/], ); say $scale; for my $aref (@aoa) { my $prefixLen; printf $pat, @$aref, $prefixLen; my $space = max_width - $prefixLen - 1; printf "%-*s|\n", $space, substr $long_string, 0, $space; }
Note that you can't do it in a single statement because the arguments to printf need to be evaluated before printf goes about the business of generating its output.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to use sprintf %n formatting pattern
by hv (Prior) on Jun 09, 2022 at 03:18 UTC | |
by GrandFather (Saint) on Jun 09, 2022 at 03:38 UTC | |
by hv (Prior) on Jun 09, 2022 at 11:54 UTC | |
by GrandFather (Saint) on Jun 09, 2022 at 12:44 UTC | |
|
Re^2: How to use sprintf %n formatting pattern
by ibm1620 (Hermit) on Jun 09, 2022 at 18:24 UTC |