in reply to sprintf and arrays of unknown (in advance) length

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re: [OT: D] Re: sprintf and arrays of unknown (in advance) length
by LanX (Saint) on Oct 13, 2018 at 00:14 UTC
    > Any chance that Perl might get something like this?

    Why? Perl has thanks to variable interpolation a huge flexibility, that's effectively a built in template language. Plus loads of operators to recombine strings.

    What's the point overloading the printf formats with more syntax if we can already have it all in a short, expressive and canonical way?

    DB<90> sub format_n { my $n= shift; printf "%b" x $n ." ", splice @_ +, 0, $n while @_ } DB<91> format_n 3,(1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0) 101 100 101 010 DB<92> format_n 4,(1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0) 1011 0010 1010 DB<93> format_n 5,(1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0) 10110 01010 10000

    (non-destructive version left as exercise)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice