David Korn writes:
> The function below reads a file of 20,000 80 byte lines into a file into
> a an index array in .07s on Linux.
>
> ==================cut here============================
> function readfile # var file
> {
> nameref var=$1
> set -f
> IFS=$'\n\n'
> var=( $(< $2))
> }
>
> ==================cut here============================
Many thanks!
I will use this. I bet others will too.
An interesting excercise left for the reader (or until I really need
it myself) is to modify this to add another optional boolean parameter
that is set to 1 if the last line of the file contains a \n. I think
it involves changing
var=( $(< $2))
to
integer fd
var=( $(fd< $2))
and using #((EOF-1)) in a test, with something like:
if (( $# == 3 )) ; then
nameref last_is_nl=$3
[[ $($fd<#) ((EOF-1)) == $'\n' ]] && last_is_nl=1 || last_is_nl=0
fi
But don't quote me on this.
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users