Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Repeat argument N times?



On Jul 29, 11:12pm, Benjamin R. Haskell wrote:
} 
} Is there a built-in way to repeat an argument N times?

Not really.  You have to admit it's a rather unusual thing to do.  It
can be done by abusing other expansions, as you've discovered.
 
} It'd be nice if I could write (something like):
} 
}     upload-files file1(N3) file2(N10) file3(N4)

You can create an array of N empty elements just by assigning to the
Nth position:

    N[100]=()

And then:

    upload-files file1$^N[1,3] file2$^N[1,10] file3$^N[1,4]

Works up to however much memory you're willing to waste on allocating
empty array slots.

There are other ways, e.g.

    upload-files file2${^${:-{1..10}}/*/}

    upload-files file2(e{'repeat 10 reply+=($REPLY)'})

though the latter requires "file2" to actually exist, which sometimes
might be a good thing.



Messages sorted by: Reverse Date, Date, Thread, Author