in reply to How to use sprintf %n formatting pattern
Alternatively you could generate the whole string then truncate it for printing:
#!/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 dog}; my $pat = "%.2f %d/%d/%d %-*s"; 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 $str = sprintf $pat, @$aref, $max_width, $long_string; print substr($str, 0, $max_width - 1), "|\n"; }
Prints:
1.......10........20........30........40........50........60 79.30 2022/1/8 The quick brown fox jumps over the dog | 394571.00 22/10/81 The quick brown fox jumps over the dog | 123456.78 12345/123/1234 The quick brown fox jumps over th|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to use sprintf %n formatting pattern
by ibm1620 (Hermit) on Jun 09, 2022 at 18:28 UTC |