Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How to use sprintf %n formatting pattern

by GrandFather (Saint)
on Jun 09, 2022 at 02:57 UTC ( [id://11144535]=note: print w/replies, xml ) Need Help??


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.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

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

    the arguments to printf need to be evaluated before printf goes about the business of generating its output

    I'm not sure what you mean by this - as far as I know the arguments are scalars, and it doesn't look at them until it needs to.

    I suspect you probably could achieve this with a tied scalar dynamically returning $max_width - $stored_width. I think it'd be a pretty insane thing to do unless there was some really strong reason not to split it into two prints, but I'm ok with a bit of insanity, I might have a go at it after some sleep.

      Consider:

      my $str = '1234567890' x 6; my $prefixLen; printf "Prefix stuff %n%s\n", $prefixLen, substr $str, 0, 35 - $prefix +Len;

      What value does substr see for $prefixLen? How can printf get the substr generated parameter generated after printf has set about generating the output?

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

        By using a sprintf parameter to determine the width:

        my $str = '1234567890' x 6; my $prefixLen; my $magicLimit = tie_me_a_scalar(sub { $max_width - $prefixLen }); printf "Prefix stuff %n%.*s", $prefixLen, $magicLimit, $str;

        Update: corrected %*s to %.*s

Re^2: How to use sprintf %n formatting pattern
by ibm1620 (Hermit) on Jun 09, 2022 at 18:24 UTC
    This shows me that I misunderstood how %n works. I had imagined that, at execution, the arguments were copied into a temporary list which the pattern then consumed, and that %n "unshifted" its value onto the head of the list, to be the next thing to be consumed. I didn't realize that the value was actually plugged into the next variable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144535]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-16 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found