From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13461 invoked by alias); 18 Jan 2017 20:18:51 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 22400 Received: (qmail 15010 invoked from network); 18 Jan 2017 20:18:51 -0000 X-Qmail-Scanner-Diagnostics: from forward2h.cmail.yandex.net by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(87.250.230.17):SA:0(-0.7/5.0):. Processed in 3.141419 secs); 18 Jan 2017 20:18:51 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_PASS,T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: kp-pav@yandex.ru X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf-ipv4.yandex.ru designates 87.250.230.17 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1484770354; bh=x0J9GDsJCFWzheNEq3O96TPwrIRlEBavKKBzrtaB4bI=; h=From:To:In-Reply-To:References:Subject:Message-Id:Date; b=EnHFnlehOuntgymZy7KhKSLV/8xr/H4ZAtDqyVZY4lJhPc5GCIGj7UFqtiCGNi8tr IScTc9b/ZHb+XkBO+SbSh9JAhv+6HSNSt94FmUlHDkzo5mBjViqv97ABiC0wsPazTg faMv/g207bsEGlAT2UhGAhnKlqXsBV9xJsWXeFZo= Authentication-Results: mxback2m.mail.yandex.net; dkim=pass header.i=@yandex.ru From: "Nikolay Aleksandrovich Pavlov (ZyX)" To: Ray Andrews , "zsh-users@zsh.org" In-Reply-To: <5ade5a22-d115-ed6f-62e2-9d69a9db20d3@eastlink.ca> References: <52be7a1a-2da8-a4b6-905a-3eea694763c3@eastlink.ca> <20170118184623.3bf91c52@ntlworld.com> <5ade5a22-d115-ed6f-62e2-9d69a9db20d3@eastlink.ca> Subject: Re: this should be easy variable expansion including globs. MIME-Version: 1.0 Message-Id: <484161484770354@web26m.yandex.ru> X-Mailer: Yamail [ http://yandex.ru ] 5.0 Date: Wed, 18 Jan 2017 23:12:34 +0300 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8 18.01.2017, 22:53, "Ray Andrews" : > On 18/01/17 10:46 AM, Peter Stephenson wrote: >>  On Wed, 18 Jan 2017 08:13:54 -0800 >>  Ray Andrews wrote: >>>  $ var1=var1 >>>  $ var2=var2 >>>  $ var3=var3 >>>  $ var99=var99 >>>  $ for f ($var*) echo $f >>  I'm going to regret this, but... > > What? Who me, make a stink about it? Never. > > Seriously, I'd do that Peter if I was creating the variables, and my > example makes it look like that, but in fact I'm trying to discover some > variables created by a program. It's the 'smartd/smart-notifier' utility > which creates a slew of variables all starting with 'SMARTD...' and > since it's run via script they all evaporate before I can see what's > available to me so I'm wanting to print out 'SMARTD*', that is, all > variables starting that way whatever they may be. I had thought of the > clumsy: > > $ set | grep SMARTD >! somefile > $ cat somefile > > ... but I expect it can be done elegantly. Why are you trying this? `set | grep …` is only bad if you need to use the results in a script, but now it looks like you are reading `somefile` manually. Alternative solutions are using `typeset -m 'SMARTD*'` (you did know about `which -m`, did not you? This is similar.) And using zsh/parameter module, there will be $parameters associative array which may be searched by indexing (find `Subscript Flags` section in man pages, though I failed to construct a useful subscript) and definitely can be processed in a cycle. > =============================== >>  The right way of doing this is to use an array. It's very similar to >>  what you've got except you refer to $var[1] rather than $var1. That >>  index means the shell knows roughly what you've got on your mind from >>  the start. >> >>  The following syntax may look too good to be true, but does work... >> >>  var=(var{1..99}) >> >>  This is equivalent to >> >>  typeset -a var >>  var[1]=var1 >>  var[2]=var2 >>  ... >> >>  The first line is there to ensure var is an array. This is a useful >>  example as it shows that the array grows as you need it to. >> >>  Now the simple "echo" you've got above can be done as >> >>  print -lr -- $var >> >>  The -l prints one entry per line. The -r stops any clever expansions so >>  you get exactly what's in the array. >> >>  For most operations, you probably need more control over what you're >>  doing with entries. Depending how complicated it gets, your main choices >>  are the following. >> >>  Process every non-empty array entry, regardless of number: >> >>  for elt in $var; do >>     # $elt in turn refers to elements of the array >>     print -r -- $elt >>  done >> >>  Process every entry whether it's empty or not --- there's no distinction >>  between the two with the values above, it's just a bit of arcanery in >>  case you need it. >> >>  for elt in "${var[@]}"; do >>     print -r -- $elt >>  done >> >>  Loop over all 99 elements of... well, anything, but in this case that >>  array: >> >>  integer i >>  for (( i = 1; i <= 99; i++ )); do >>     print -r -- $var[i] >>  done >> >>  If you nonetheless still want to do some completely different, someone >>  else will no doubt be along in a minute for the usual loooooooong >>  argument. >> >>  pws