* The Z Shell Manual, zsh/mapfile documentation correction
@ 2013-09-01 22:34 Jim
2013-09-02 9:20 ` Peter Stephenson
0 siblings, 1 reply; 2+ messages in thread
From: Jim @ 2013-09-01 22:34 UTC (permalink / raw)
To: zsh-workers
[-- Attachment #1: Type: text/plain, Size: 1357 bytes --]
Hello,
While working on a zsh script, I researched using module zsh/mapfile to
read a file into an array for further processing. Trying the example from *The
Z Shell Manual*(Section 22.15) I found that it didn't work as stated. It
failed to read in blank lines. I had also noticed an extra element in the
array.
Example: array=("${(f)mapfile[*filename*]}")
also
egrep -v '^$' filename | wc -l yielded n lines
print ${#array} yielded n+1 elements
Not that I needed the blank lines, in this case, but it did not read them
into the array. After some web searching I found that this was discussed
on zsh-users in the 2008 thread *reading a file into an array. mapfile? (f)?
*. URL: http://www.zsh.org/mla/users/2008/msg00849.html. The fix was
actually stated in URL: http://www.zsh.org/mla/users/2008/msg00874.html.
Correction: array=("${(f@)mapfile[*filename*]}")
The discussion also stated the fact that an extra element(blank) is added
to the end of the array. It would be nice to have a statement in *The Z
Shell Manual* that the array will contain an additional element. I'm not
sure I agree that this should be the result, but I'll leave that to others
more knowledgeable about the subject.
Versions of the manual I checked are:
4.3.17 -- This is also the version of zsh I'm using(OS: Debian Wheezy).
5.0.2-dev-0
Sincerely,
Jim
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: The Z Shell Manual, zsh/mapfile documentation correction
2013-09-01 22:34 The Z Shell Manual, zsh/mapfile documentation correction Jim
@ 2013-09-02 9:20 ` Peter Stephenson
0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2013-09-02 9:20 UTC (permalink / raw)
To: zsh-workers
On Sun, 01 Sep 2013 17:34:38 -0500
Jim <linux.tech.guy@gmail.com> wrote:
> While working on a zsh script, I researched using module zsh/mapfile to
> read a file into an array for further processing. Trying the example from *The
> Z Shell Manual*(Section 22.15) I found that it didn't work as stated. It
> failed to read in blank lines. I had also noticed an extra element in the
> array.
>
> Example: array=("${(f)mapfile[*filename*]}")
>
> also
>
> egrep -v '^$' filename | wc -l yielded n lines
> print ${#array} yielded n+1 elements
>
> Not that I needed the blank lines, in this case, but it did not read them
> into the array. After some web searching I found that this was discussed
> on zsh-users in the 2008 thread *reading a file into an array. mapfile? (f)?
> *. URL: http://www.zsh.org/mla/users/2008/msg00849.html. The fix was
> actually stated in URL: http://www.zsh.org/mla/users/2008/msg00874.html.
>
> Correction: array=("${(f@)mapfile[*filename*]}")
>
> The discussion also stated the fact that an extra element(blank) is added
> to the end of the array. It would be nice to have a statement in *The Z
> Shell Manual* that the array will contain an additional element. I'm not
> sure I agree that this should be the result, but I'll leave that to others
> more knowledgeable about the subject.
diff --git a/Doc/Zsh/mod_mapfile.yo b/Doc/Zsh/mod_mapfile.yo
index 98114ae..96e0568 100644
--- a/Doc/Zsh/mod_mapfile.yo
+++ b/Doc/Zsh/mod_mapfile.yo
@@ -28,9 +28,12 @@ referenced may not be written or deleted.
A file may conveniently be read into an array as one line per element
with the form
-`var(array)tt(=LPAR()"${(f)mapfile[)var(filename)tt(]}"RPAR())'.
-The double quotes are necessary to prevent empty lines from being
-removed.
+`var(array)tt(=LPAR()"${(f@)mapfile[)var(filename)tt(]}"RPAR())'.
+The double quotes and the `tt(@)' are necessary to prevent empty lines
+from being removed. Note that if the file ends with a newline,
+the shell will split on the final newline, generating an additional
+empty field; this can be suppressed by using
+`var(array)tt(=LPAR()"${(f@)${mapfile[)var(filename)tt(]%$'\n'}}"RPAR())'.
)
enditem()
pws
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2013-09-02 9:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-01 22:34 The Z Shell Manual, zsh/mapfile documentation correction Jim
2013-09-02 9:20 ` Peter Stephenson
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).