Hi,
Andy Wokula wrote:
> 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
this would be the correct result. What I think happens is, because of
the equal precedence of "." and "+", "." and "+" are evaluated from left
to right:
:echo 'tot is ' . eval(hi . 100+400+kk+200)
:echo 'tot is ' . eval((hi . 100)+400+kk+200)
:echo 'tot is ' . eval(('10+100')+400+kk+200)
:echo 'tot is ' . eval( 10 +400+kk+200))
:echo 'tot is ' . 1610
:echo 'tot is ' . '1610'
:echo 'tot is 1610'
tot is 1710
Regards,
Jürgen
--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)
--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php