When I using dictionary, how can I add items using my input order?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

When I using dictionary, how can I add items using my input order?

Sand Glass
eg:
function IlaAutoGenerate ()
    let wides = 0
    let signals = {}
    for line in getline(1, line("$"))
       if line !~ '^\s*$'
          let line = substitute(line, '^\s*', "", "")
          let sig_msb = 0
          let wides += sig_msb + 1
          let sig_name = substitute(line, '\s*\(;\|,\).*$', "", "")
          call extend(signals, {sig_name : sig_msb}, "force")
       endif
    endfor
    for sig_i in keys(signals)
            call setline(curr_line, "reg ila_".sig_i.";")
            let curr_line += 1
    endfor
endfunction
I input the following:
sig_b;
sig_d,
    c;
    aa;
tmu_a;

I expected output:(or the inverse order as my input)
reg ila_sig_b;
reg ila_sig_d;
reg ila_c;
reg ila_aa;
reg ila_tmu_a;

But actually is:
reg ila_c;
reg ila_sig_b;
reg ila_aa;
reg ila_sig_d;
reg ila_tmu_a;

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: When I using dictionary, how can I add items using my input order?

Luc Hermitte
Hi,

You can't, dictionaries are just hash tables, there is no ordering you can count upon.

You'll have either to record a list of the keys in the order you desire, or give up on the big dictionary and use a list instead -- either a list of lists, or a list of dictionaries.


If you don't need the dictionary for anything else, no need to extract all the information.

You should be able to achieve your result the following (which would be more efficient BTW, but I doubt you'd perceive any difference on real code)

   let lines = filter(getline(1, '$'), 'v:val !~ "^\\s*$"')
   call map(lines, 'substitute(v:val, "^\\s*", "", "")')
   let names = map(copy(lines), "substitute(v:val, '\\s*[;,].*$', '', '')")
   let newlines = map(copy(names), '"reg ila_".v:val.";"')
   call setline(curr_line, newlines)

HTH,

--
Luc Hermitte

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.