juliet_w schrieb:
> Hi guys,
>
> Here is a basic use of eval in vimscript that produces results that
> I don't understand.
>
>
> let kk = 1000
> echo 'tot is ' . eval(100+400+kk+200)
> " result is correct : tot is 1700

eval() is not needed here:
    :echo 'tot is ' . (100+400+kk+200)
    tot is 1700

but it works, because
    eval(1700) == 1700

> let kk =1000
> let hi ='10+'
> echo 'tot is ' . eval( hi . 100+400+kk+200)
> " result  is wrong : tot is 1610
>
> Why result is wrong in the second case?
> "hi' and kk values are added , but result should be 1710

Strings are converted to numbers before adding the results.
Numbers are converted to strings before concatenating the results.
"." and "+" have the same precedence.

    :echo 1 + 2 . 3
    :echo   3   . 3
    33

    :echo 2 . 3 + 1
    :echo  23   + 1
    24

    :let kk = 1000
    :let hi = '10+'

Missing parens added:
    :echo 'tot is ' . (hi . (100+400+kk+200))
    tot is 10+1700

    :echo 'tot is ' . eval(hi . (100+400+kk+200))
    tot is 1710

Step by step:
    :echo 'tot is ' . eval(hi . (100+400+kk+200))
    :echo 'tot is ' . eval(hi . 1700)
    :echo 'tot is ' . eval(hi . '1700')
    :echo 'tot is ' . eval('10+1700')
    :echo 'tot is ' . 1710
    :echo 'tot is ' . '1710'
    :echo 'tot is 1710'
    tot is 1710

Or (not recommended):
    :let hi2 = '+10'
    :echo 'tot is ' . eval(100+400+kk+200 . hi2)
    tot is 1710

Also interesting (but does not influence the above result):
    " wasn't there a thread about this (?):
    :echo 0 + +10
    10

    :echo 0 + '+10'
    0
!   ^-- strange

    :echo 0 + -10
    -10

    :echo 0 + '-10'
    -10

-- 
Andy

-- 
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

Reply via email to