From: Marc Chantreux <khatar@phear.org>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: zsh-users@zsh.org
Subject: (some tips about variables) Re: avoid eval?
Date: Tue, 13 Mar 2018 21:39:22 +0100 [thread overview]
Message-ID: <20180313203922.37npscl5jzequet3@prometheus.u-strasbg.fr> (raw)
In-Reply-To: <32661718-eaa2-a270-9b88-69156f7ddfe6@eastlink.ca>
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
next prev parent reply other threads:[~2018-03-13 20:39 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-11 20:48 Ray Andrews
2018-03-11 22:53 ` Marc Chantreux
2018-03-12 0:43 ` Ray Andrews
2018-03-13 2:13 ` Ray Andrews
2018-03-13 2:17 ` Eric Cook
2018-03-13 16:36 ` (some tips about variables) " Marc Chantreux
2018-03-13 17:50 ` Ray Andrews
2018-03-13 20:39 ` Marc Chantreux [this message]
2018-03-13 21:50 ` Ray Andrews
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=20180313203922.37npscl5jzequet3@prometheus.u-strasbg.fr \
--to=khatar@phear.org \
--cc=rayandrews@eastlink.ca \
--cc=zsh-users@zsh.org \
/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).