Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

(some tips about variables) Re: avoid eval?



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




Messages sorted by: Reverse Date, Date, Thread, Author