[j-nsp] repeat a character in SLAX certain number of times

Phil Shafer phil at juniper.net
Thu Mar 22 17:05:05 EDT 2018


Martin T writes:
>..or for example, in Bash I could do "for i in $(seq $x); do echo -n =; done; echo". What is the most elegant way to do this in SLAX? At the moment I solved it with this:

You can brute force it with a loop or a recursive function (below),
but best to use the exslt extension function "str:padding":

    var $x = str:padding(168, "=");

FWIW, the brute force patterns are:

    var $x = {
        for-each (1 ... $count) {
            expr $char;
        }
    }

    var $x = call padding($char, $count);

where:

template padding ($char = " ", $count = 0) {
    if ($count > 0) {
        expr $char;
        call padding($char, $count = $count - 1);
    }
}

Running these with the profiler should easily convince you that the
extension function is the best answer.  Just showing the others for
educational purposes.  And because the conference call I'm on is
rather dull.

Thanks,
 Phil


More information about the juniper-nsp mailing list