Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: reading a file into an array. mapfile? (f)?
- X-seq: zsh-users 13258
- From: Peter Stephenson <pws@xxxxxxx>
- To: "zsh users mailing list" <zsh-users@xxxxxxxxxx>
- Subject: Re: reading a file into an array. mapfile? (f)?
- Date: Fri, 19 Sep 2008 12:00:54 +0100
- In-reply-to: <6cd6de210809190339g57a4e722u5ad1371e858c8501@xxxxxxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <6cd6de210809171953pd956d75gd4b3609ac036fe0@xxxxxxxxxxxxxx> <080917214418.ZM27616@xxxxxxxxxxxxxxxxxxxxxx> <6cd6de210809181747kc0f0a5bt4489f534589725a8@xxxxxxxxxxxxxx> <20080919100725.5c471019@news01> <6cd6de210809190339g57a4e722u5ad1371e858c8501@xxxxxxxxxxxxxx>
"Rocky Bernstein" wrote:
> Further tests show that the behavior seems to be to drop blank lines but put
> one at the end of the file.
Apologies, you're right; it seems you *do* need the @ flag, but my test
was too simple to show it.
array=("${(f@)mapfile[foo.txt]}")
I'm not entirely sure why that is but I must be misremembering the (f)
rules.
Also, it seems you always get an extra blank at the end: I think that's
because mapfile returns the complete file including the "\n" and the (f)
flag splits on that "\n" giving you a blank after it. This should be
entirely predictable, however (I hope): you get that last blank line if
there was a final newline, and if you don't get it there wasn't a final
newline.
I think that's the one difference from "${(f@)$(<foo.txt)}": in that
case a final newline is always stripped, but consequently you can't tell
if there was one there.
--
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