From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh users list <zsh-users@sunsite.dk>
Subject: Re: The amazing array feature in zsh
Date: Wed, 16 Aug 2006 00:12:07 -0700 [thread overview]
Message-ID: <060816001207.ZM9564@torch.brasslantern.com> (raw)
In-Reply-To: <20060816044842.44532.qmail@web8403.mail.in.yahoo.com>
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.
next prev parent reply other threads:[~2006-08-16 7:13 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-08-16 4:48 sac
2006-08-16 6:10 ` John Reese
2006-08-16 7:12 ` Bart Schaefer [this message]
2006-08-16 10:07 ` sac
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=060816001207.ZM9564@torch.brasslantern.com \
--to=schaefer@brasslantern.com \
--cc=zsh-users@sunsite.dk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).