Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: The amazing array feature in zsh
- X-seq: zsh-users 10613
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh users list <zsh-users@xxxxxxxxxx>
- Subject: Re: The amazing array feature in zsh
- Date: Wed, 16 Aug 2006 00:12:07 -0700
- In-reply-to: <20060816044842.44532.qmail@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <20060816044842.44532.qmail@xxxxxxxxxxxxxxxxxxxxxxxxx>
On Aug 15, 9:49pm, sac wrote:
}
} I discovered this amazing feature of array recently,
} assigning values just like we do in some high level
} language.
Gosh, and here all these years I thought the shell *was* a high-level
language. :-)
} Here is a example,
}
} files=() # initialize to null
} for mfile in `svn stat $1 | grep '^M' | awk '{
} print $2 }'`
} do
} files+=($mfile)
} done
It gets better ... recent versions of zsh can do this:
for svncode svnfile in $(svn stat $1)
do
case $svncode in
(M) files+=($svnfile);;
esac
done
} print -c $files # print with tabs
}
} Notice tha assignment to the array variable +=.
I don't know why this isn't discussed under "Array Parameters". It
gets a passing mention earlier (as John R. pointed out) but that's
only about scalars. Then it's mentioned in a different context under
"Subscript Parsing":
It is possible to avoid the use of subscripts in assignments to
associative array elements by using the syntax:
aa+=('key with "*strange*" characters' 'value string')
This adds a new key/value pair if the key is not already present,
and replaces the value for the existing key if it is.
} And actually this can be iterated like,
}
} for file in $files
} do
} <do something with $file>
} done
}
} I dont know if any other shell provides similar
} feature, but this one is too good and useful, and
} makes the use of array in shell very easy. But I dont
} think this is documented in zsh, atleast I couldn't
} find it.
The behavior as in "for file in $files" context is described in the
"Parameter Expansion" section:
If NAME is an array parameter, and the KSH_ARRAYS option is not
set, then the value of each element of NAME is substituted, one
element per word. Otherwise, the expansion results in one word
only; with KSH_ARRAYS, this is the first element of an array. No
field splitting is done on the result unless the SH_WORD_SPLIT
option is set.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author