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

Re: reading a file into an array. mapfile? (f)?



On Thu, 18 Sep 2008 20:47:00 -0400
"Rocky Bernstein" <rocky.bernstein@xxxxxxxxx> wrote:
> On Thu, Sep 18, 2008 at 12:44 AM, Bart Schaefer
> <schaefer@xxxxxxxxxxxxxxxx>wrote:
> > It's much simpler than you seem to believe:
> >
> > lines=( ${(f)mapfile[/path/to/file]} )
> 
> 
> Doesn't seem to work if the file (/path/to/file) has 0-length lines. These
> don't get stored in the array (lines).

Try

lines=( "${(f)mapfile[/path/to/file]}" )

Explanation:  without the quotes, elements that turn into an empty string
are removed.  (f) includes the effect of the (@) flag, which is a
generalised equivalent of the behaviour of "${array[@]}", so simply adding
the quotes is enough to get the empty lines.

I'd better update the change I made to the mapfile documentation.

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



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