Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
A challenge – non-extendedglob whitespace trimming
- X-seq: zsh-users 23717
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: A challenge – non-extendedglob whitespace trimming
- Date: Wed, 24 Oct 2018 17:12:38 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=vyi7FknZGLh4mua+j0hBiRcDdFvo5z/HUTg933SxpeM=; b=M8n3wABU9U+GWe6rv5J2BYX92zM9BKqIIiZDXZdHkVrVjwnjhS0h6yX/8lnDw9eNL9 kgRlFdAUMdIgyMOkIA1WZ//uqoqrcWNZ7fJOKwcasUSl9AZK5Bgbmld7rPQhG5OOgy5B 7I/IPIEF7bjemmCyTv9P71Gvryh8fPGQ8Ydm/dtp7cjHP5aFBrFw46jQzRYzZWXX67dj QCwZ2LJNJsBtrstNr4tAh8oVwJp9vSQpI620G3VP13g/asv6X8ziehqXnBsuY+k31le8 lHEmphE2A7CHl42wo4s25O7gWc0QpJPhXHsWPafUnQ7DCOCo3r8CeWBLsVojZClGQpbX RUZw==
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
Hello!
I think this is a valid challenge:
1. One can do the following to strip whitespace from string $str:
str=${${str##[[:space:]]##}%%[[:space:]]##}
This uses ## operator and thus requires setopt extendedglob.
2. Extendedglob-free code that does the same:
str="${str#"${str%%[! $'\t']*}"}" # leading whitespace
str="${str%"${str##*[! $'\t']}"}" # trailing whitespace
It's tricky, but the main thing one has to notice to grasp this: The
inner ${str%%...} substitution will match from end to beginning of
string till last non-whitespace. So it will left only leading
whitespace. Which is then removed from start of the string with
#-subst.
3. I have an array `pairs', holding elements like this: pairs=( "a ->
b" " c ->d " ), etc. I want to obtain array with additional split
on "->" (so it is nicely fitting into a hash, key-a val-b, etc.).
Following is an extendedglob-dependant code that performs the split
and also trims leading and trailing whitespace from all elements:
pairs=( "${(@)${(@)${(@s:->:)pairs}##[[:space:]]##}%%[[:space:]]##}" )
4. Is there a extendedglob-free (not depending on it) version of the
above point 3.?
--
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org
Messages sorted by:
Reverse Date,
Date,
Thread,
Author