The D language has the nesting  %( %) variadic array format specifiers (see std.format.formattedWrite). (I haven't followed up, but D often tracks C/C++ features, so the latest versions of these languages may have these specifiers also.) Any chance that Perl might get something like this?

File t_format_varfld_1.d:
// t_format_varfld_1.d 12oct18waw // for pm#1223893 // compile: // dmd t_format_varfld_1.d // invoke: // t_format_varfld_1 n 1 [ 0 1 ... ] // where: // n group size of output string. // 1 [ 0 1 ... ] bits to be grouped. can be any integer; only // least significant bit will be printed. immutable USAGE = " invoke: t_format_varfld_1 n 1 [ 0 1 ... ] where: n required: group size of output string. 1 [ 0 1 ... ] one or more bits to be grouped. can be any integer; only least significant bit will be processed. "; int main (string [] args) { import std.stdio : writeln, writefln; import std.conv : to; import std.array : array, popFront, front; import std.algorithm : map; args.popFront; // discard program name if (! args.length) { writeln("no group size given", USAGE); return(1); } const N = args.front.to!size_t; args.popFront; // discard group size if (! args.length) { writeln("no bits/ints given", USAGE); return(1); } auto bits = args.map!"a.to!uint".array; auto str = str_groups(N, bits); writefln("%s -> '%s'", bits, str); return(0); } // end main() auto str_groups (T = typeof(N), B = typeof(bits[])) (in T n, B [] b) { import std.format : format; import std.range : chunks; import std.algorithm : map; return b // for input array... .map !"a &= 1" // 1. only process lsb of each charact +er .chunks(n) // 2. partition array into sub-arrays .format !"%(%(%b%) %)" // 3. format each sub-array into strin +g ; // 4. return formatted string of strin +gs }
Output:
C:\@Work\DMD2\posts\Anonymous Monk\1223893>t_format_varfld_1 3 1 0 1 + 1 0 0 1 0 1 0 1 0 [1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0] -> '101 100 101 010' C:\@Work\DMD2\posts\Anonymous Monk\1223893>t_format_varfld_1 4 1 0 1 + 1 0 0 1 0 1 0 1 0 [1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0] -> '1011 0010 1010' C:\@Work\DMD2\posts\Anonymous Monk\1223893>t_format_varfld_1 5 1 0 1 + 1 0 0 1 0 1 0 1 0 [1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0] -> '10110 01010 10'


Give a man a fish:  <%-{-{-{-<


In reply to [OT: D] Re: sprintf and arrays of unknown (in advance) length by AnomalousMonk
in thread sprintf and arrays of unknown (in advance) length by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.