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?
Output:// 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 }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |