Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 938057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:34:46+00:00 2026-05-15T21:34:46+00:00

I have a script that is intended to check whether some value (e.g. option

  • 0

I have a script that is intended to check whether some value (e.g. option or function argument) matches some model. I want my script to be able to check recursive data structures. So the question is: is there more efficient way then iterating over some list that contains references to already checked Lists and Dictionaries. Example code:

function s:AlreadyChecked(arg, checkedlst)
    if type(a:arg)!=type([]) && type(a:arg)!=type({})
        return 0
    endif
    for obj in a:checkedlst
        if a:arg is obj
            return 1
        endif
    endfor
    call add(a:checkedlst, a:arg)
    return 0
endfunction

Seeking for a way to sort checkedlst (that is to compare references, but not values found by them) or even to use a hash.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:34:46+00:00Added an answer on May 15, 2026 at 9:34 pm

    As I’m guessing you’ve already discovered, Vim doesn’t allow List or Dictionary variables to be used as dictionary keys. That means you can’t, for example, populate a “checked” dictionary like this:

    " Unless k is a String, this won't work.
    :let checked[k] = 1
    

    It also lacks a straightforward way to generate a unique String from a List or Dictionary, so this isn’t reliable either:

    :let checked[ string(k) ] = 1
    

    A better approach is to mark the data structures themselves instead of trying to build a hashtable. If you don’t mind temporarily making your data structures read-only, one way to do that is to use :lockvar:

    :let someDict = {}
    :let someDict['foo'] = [1, 2, 3]
    :lockvar 1 someDict
    

    That marks someDict as read-only. (The 1 limits the locking to the top level of the Dictionary, so nested structures aren’t automatically locked.) A variable’s lock state can be inspected like this:

    :echo islocked('someDict')
    1
    
    :echo islocked("someDict['foo']")
    0
    
    :echo islocked("someDict['foo'][0]")
    0
    

    Unlocking is just as easy:

    :unlockvar 1 someDict
    

    So now we have a technique for marking individual levels of nested data structures as “checked”, a way to query whether or not a particular level is marked, and a way to remove all the marks when we’re done. Putting it all together, AlreadyChecked() can be modified like so:

    function! s:AlreadyChecked(arg, checkedlst)
    
        if type(a:arg)!=type([]) && type(a:arg)!=type({})
            return 0
        endif
    
        " If this particular List or Dictionary has already been checked, just
        " return true immediately.
        "
        if islocked('a:arg')
            echo "Already checked."
            return 1
        endif
    
        " Lock the List or Dictionary to mark this item as already
        " checked. Note that only the top level of the List or Dictionary
        " is locked; values are not locked.
        "
        lockvar 1 a:arg
    
        " Remember everything we've locked, so it can be unlocked once
        " we're done.
        "
        call add(a:checkedlst, a:arg)
    
        return 0
    
    endfunction
    

    Once you’re done checking, just remove all the locks:

    for obj in a:checkedlst
        unlockvar 1 obj
    endfor
    

    Hope this helps. It’s a hackish abuse of the locking facility, but perhaps it will do what you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script that is intended to run on my local machine
I have a script that I'm working on that is intended to remove the
I have a script that is intended to be run by multiple users on
I have a few simple scripts that are intended to daisy chain together to
I have script that reads remote file content and writes it to local server.
I have this script that run to fix my menu bar to the browser
I have a script that recursively loops through all the sub directories and compresses
I have a script that submits data via an ajax POST. It is called
I have following script that executes all the .reg files in the current directory.
I have a script that obtains information about the current folders and subfolders within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.