Loops/For: Difference between revisions

1,447 bytes added ,  5 days ago
Added Vim Script solution
(Add ed example)
(Added Vim Script solution)
Line 4,001:
end
endmodule
</syntaxhighlight>
 
=={{header|Vim Script}}==
 
The two-loop approach determined in the description is solved in a similar manner to Python (initial solution) and Nim, using the inbuilt function, [https://vimhelp.org/builtin.txt.html#range%28%29 range()], which technically may be considered a ''for each'' loop. Nonetheless, it is an incrementing integer solution like many others provided. The script will append the output to the end of the current buffer. It uses inbuilt functions [https://vimhelp.org/builtin.txt.html#append%28%29 append()], [https://vimhelp.org/builtin.txt.html#line%28%29 line()], [https://vimhelp.org/builtin.txt.html#setline%28%29 setline()], and [https://vimhelp.org/builtin.txt.html#getline%28%29 getline()]:
 
<syntaxhighlight lang="vimscript">
for i in range(1, 5)
call append(line('$'), '')
for j in range(1, i)
call setline(line('$'), getline('$') .. '*')
endfor
endfor
</syntaxhighlight>
 
If the constraint to use two ''for'' loops was absent, the more concise Vim Script solution &#x2015; ''similar to idiomatic Python solution #2'' &#x2015; would be (this time using builtin functions [https://vimhelp.org/eval.txt.html#%3Aechoconsole echoc[onsole<nowiki>]</nowiki>] and [https://vimhelp.org/builtin.txt.html#repeat%28%29 repeat()] to demonstrate another approach to generating the output, i.e., to the console):
 
<syntaxhighlight lang="vimscript">
for i in range(1, 5)
echoc repeat('*', i)
endfor
</syntaxhighlight>
 
31

edits