Perl has fluidity between lists and dictionaries, such that a
dictionary can be considered and manipulated as a list, where pairs
become key/vals.  I don't see this in Vim, but this is simple enough:

let list1 = split( "from begin end select" )
while len( list1 )
  let [ key, val ] = remove( list1, 0, 1 )
  let dict[key] = val
endwhile


Secondly, I don't think I've ever seen a dictionary/associative array
that has any internal sort order than then hash table required to
index the keys.  So, creating a list from a dict will always have an
unexpected order unless explicitly sort()'d.


P.S. I'll put in another plug for a Perl-like "splice" function to do
things, among others, take a list two elements at a time.  Below is a
version until then:

" I was hoping this would work the same as "remove()",
" but it fails saying "Less targets than list items".  Is there something to do
" that would allow returning a list into a [a,b,c] LHS?
"
let list1 = [ 'a', 1, 'b', 2, 'c', 3 ]
while len( list1 )
        "let [ key, val ] = Splice( list1, 0, 2 )
        let [ key, val ] = remove( list1, 0, 1 )
        echo 'k ' . key . ', val ' . val
endwhile



" Replace elements in list1,  (offset, offset+len ) with trailing list
" (adding or removing as necessary)
"
function! Splice( list1, offset, len, ... )

        "let list1 = copy(a:list1)
        let list2 = []
        let extracted = []

        if a:0 > 0
                for i in range( 1, a:0 )
                        call add( list2, a:{i} )
                endfor
        endif

        if a:len > 0
                let end = min( [ a:offset + a:len, len( a:list1 ) - 1 ] )
                let extracted = remove( a:list1, a:offset, end )
        elseif a:len < 0
                let end = max( [ a:offset, len( a:list1 ) + a:len ] )
                let extracted = remove( a:list1, a:offset, end )
        endif

        call extend( a:list1, list2, a:offset )

        return extracted

endfunction

let l2 = []
let l = [ 1,2,3,4,5 ]
call Splice( l, 0, 0, 'a', 'b' )
echo string(l)

let l = [ 1,2,3,4,5 ]
let l2 = Splice( l, 1, 0, 'C', 'D' )
echo string(l)
echo string(l2)

let l = [ 1,2,3,4,5 ]
let l2 = Splice( l, 3, -1, 'ee', 'ff' )
echo string(l)
echo string(l2)

let l = [ 1,2,3,4,5 ]
let l2 = Splice( l, 2, -2, 'ee', 'ff' )
echo string(l)
echo string(l2)

let l = [ 1,2,3,4,5 ]
let l2 = Splice( l, 1, 1, 'ee', 'ff' )
echo string(l)
echo string(l2)


let l = [ 1,2,3,4,5 ]
let l2 = Splice( l, 1, 3, 'gg', 'hh' )
echo string(l)
echo string(l2)






On 4/12/06, David Fishburn <[EMAIL PROTECTED]> wrote:
>
> The sqlcompletion and syntaxcompletion plugins are available with Vim70d.
>
> They both return Lists with the required data.
>
> I would like to add additional information by returning a Dictionary
> instead, so the OMNI preview window contains useful information.
>
> The syntax plugin retrieves the syntax items via the :syntax list command.
> This is redirected into a variable and then some regex's are run against it.
> The net result is a space separate list of syntax items.
>         "from begin end select"
>
> Then typically I create a list, sort it and return it to the user via the
> omni completion popup:
>     let compl_list = sort(split(syn_list))
>
> I was wondering if there was an easy way to convert:
>         "from begin end select"
>
> Into a Dictionary something like:
>             {'word':'from', 'menu':'from', 'kind':'f', 'info':'sqlKeyword'}
>             {'word':'begin', 'menu':'begin', 'kind':'f',
> 'info':'sqlKeyword'}
>             {'word':'end', 'menu':'end', 'kind':'f', 'info':'sqlKeyword'}
>             {'word':'select', 'menu':'select', 'kind':'f',
> 'info':'sqlKeyword'}
>
> Reading through the help and the functions available, I didn't see any.
> Will I have to perform a brute force technique and use a for each loop?  I
> am looking for alternatives since the sqlcompletion plugin often does not
> have control over the format it will receive the data in, so I am trying to
> be as flexible as possible.
>
> If I create a List out of Dictionary items, can it still be sorted?
>
> TIA,
> Dave
>
>
>

Reply via email to