From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11434 invoked by alias); 13 Mar 2018 20:39:40 -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: List-Unsubscribe: X-Seq: 23240 Received: (qmail 24947 invoked by uid 1010); 13 Mar 2018 20:39:40 -0000 X-Qmail-Scanner-Diagnostics: from aurora-borealis.phear.org 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(94.242.205.164):SA:0(-1.9/5.0):. Processed in 2.477027 secs); 13 Mar 2018 20:39:40 -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=-1.9 required=5.0 tests=BAYES_00,SPF_PASS, T_DKIM_INVALID,T_RP_MATCHES_RCVD autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: khatar@phear.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | X-Virus-Scanned: amavisd-new at phear.org DKIM-Filter: OpenDKIM Filter v2.10.3 aurora-borealis.phear.org 960DA10064 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=phear.org; s=20180217; t=1520973569; bh=TdH5ak+QJsBDFEu4kZXgBU/2PVV202oSkXXCtJej69Y=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=PH3SaPiLggWUrlZzKzgmzP8GazKb8lxLNhqnKj2r3ITat9jxqGvlLIxPU2TU1h6RA 9y/boqMLHKdQeIqhOnWMZ0u9hMvIymUeeXlnZDMkHsqlbHjDZ8E8iHTgIVuIUsprEM ehiPRWLTmD6nwpItpbb0McEUN3WyJNHJ74jd3KIAT+vItUHVzJfhMhQOSk1+3i/MAx Bw780Tz7GJ0N1+sCES9QyFL0UC7ilcI11lg4ByxSXaY1dJv4JOiwk+55CgARrgMfLr 1HVSzQdhR+Hvbw6m2UGO2Vksf2VdTFo/gjrwonDWwLX5R48YwesQIY2kyhScQE2CH2 PTDZTqFtp+RZw== Date: Tue, 13 Mar 2018 21:39:22 +0100 From: Marc Chantreux To: Ray Andrews Cc: zsh-users@zsh.org Subject: (some tips about variables) Re: avoid eval? Message-ID: <20180313203922.37npscl5jzequet3@prometheus.u-strasbg.fr> References: <059f4f08-7f7f-25d8-dfeb-1653e3c8ba95@eastlink.ca> <20180311225321.shqrx7idd57ie62d@prometheus.u-strasbg.fr> <20180313163634.s4qvlfhdzqplxn4s@prometheus.u-strasbg.fr> <32661718-eaa2-a270-9b88-69156f7ddfe6@eastlink.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <32661718-eaa2-a270-9b88-69156f7ddfe6@eastlink.ca> User-Agent: NeoMutt/20170113 (1.7.2) hello, > Well, it it can be *that* local, just for one loop, then that makes things > much simpler. yes it can. another way to keep things very local is to use anonymous functions so you can write in the middle of your script () { local user for user { print "hello $user" } } bob joe ted print "finally: hello ${user:-world}" which gives you hello bob hello joe hello ted finally: hello world > Still there are mysteries: > function test () wow .. don't overwrite an existing command, ever! (note: it would be cool to rise a warning when you do that) > local IFS=$'\n' > echo $path[2] > tty=( `stty size` )         # Grab the size of the terminal. > echo $tty > echo $tty[1] > local IFS=' ' > echo $path[2] > tty=( `stty size` )         # Grab the size of the terminal. > echo $tty > echo $tty[1] > } > > $ . ./test; test > /aWorking/Zsh/System > 52 80 > 52 80 > /aWorking/Zsh/System > 52 80 > 52 > ... $path is space-separated, yet it is 'immune' to IFS issues. Array $tty, you can check the type of a variable using the (t) modifier. print ${(t)path} array-special > it is not immune to IFS. I suspect that's because maybe $path is not an > array, can you please show a little case where something isn't working ? i don't understand the problem you're facing. reading the values of a command with multiple values, a lovely thing about zsh (unique, i think) is that you can use read at the end of a pipe so for example: # mocking ssty stty () print 20 30 local -A stty stty size | read stty\[{x,y}] print $stty[x] # prints 20 > how does one tell one from the other? It seems counter intuitive that > "$tty[1]" and "$tty" could mean the same thing in any situation. # mocking ssty stty () print 20 30 stty=( `stty size` ) i=0; for el ($stty) print $[i++] : $el 0 : 20 1 : 30 the thing works the way you expect because IFS is the default one: "$'\t' ". now if you set IFS you got # mocking ssty IFS=: stty () print 20 30 stty=( `stty size` ) i=0; for el ($stty) print $[i++] : $el because zsh found no ':' to split the result the whole line is stored as $stty[1]. in this case as $stty contains only $stty[1]: 0 : 20 30 > Splitting > issues seem the one thing that is always hard to get right, > there seem to be dozens of variations on the theme. IFS is the field separator, $'\n' is the record separator so if you set IFS to $'\n' you can't basically split nothing captured by read. you can change the record separator with 'read -d' I show you an example in the previous mail slurp () IFS=$'\n' read -d '' -A $1 hosts=() grep '^[^#]' /etc/hosts | slurp hosts print $hosts[1] outputs 127.0.0.1 localhost regards marc