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 7129303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:13:42+00:00 2026-05-28T11:13:42+00:00

I’m using Tcl. I have a sorted list of real numbers. Given the number

  • 0

I’m using Tcl. I have a sorted list of real numbers. Given the number n I need to find an index of the list element which is:

  • either less of equal to n;
  • or greater than n.

Is there any standard way to do this? lsearch expects exact match and can’t be used.

  • 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-28T11:13:43+00:00Added an answer on May 28, 2026 at 11:13 am

    With Tcl 8.6 (still in beta) lsearch will do what your asking, the -sorted and new -bisect options allow the following:

    -bisect
    Inexact search when the list elements are in sorted order. For an increasing list the last index where the element is less than
    or equal to the pattern is returned. For a decreasing list the last
    index where the element is greater than or equal to the pattern is
    returned.

    For Tcl versions prior to 8.6 your going to have to roll your own code, given that the list is sorted it should be fairly straightforward to write a binary search with the properties that you require, Rosetta code here contains a description of a pure binary search and also a Tcl implemention. You should be able to use this as your starting point.

    Here is a very quick version I created, it returns the index of either the value you search for or the value greater than it. The exception to watch for is the end of the list, searching for a value beyond the largest element returns the largest element. It’s only had minimal testing so if you do use it do some additional tests! I also don’t stop if the search finds the value, if this is likely to happen often you may want to optimize for this.

    set lst [lsort -real [list 1.2 3.4 5.4 7.9 2.3 1.1 0.9 22.7 4.3]]
    puts $lst
    
    # Assumes that lst is sorted in ascending order
    proc bisect { lst val } {
    
        puts "Looking for $val in $lst"
    
        set len [llength $lst]
    
        # Initial interval - the start to the middle of the list
        set start 0
        set end [expr $len - 1]
        set mid [expr $len / 2]
        set lastmid -1
    
        while { $mid != $lastmid } {
            if { [expr $val <= [lindex $lst $mid]] } {
                # val lies somewhere between the start and the mid
                set end $mid
    
            } else {
                # val lies somewhere between mid and end
                set start [expr $mid + 1]
            }
    
            set lastmid $mid
            set mid [expr ($start + $end ) / 2]
        }
    
        return $mid
    }
    
    set res [bisect $lst 2.4]
    puts "found [lindex $lst $res] at index $res"
    
    set res [bisect $lst -1]
    puts "found [lindex $lst $res] at index $res"
    
    set res [bisect $lst 999]
    puts "found [lindex $lst $res] at index $res"
    
    set res [bisect $lst 1.2]
    puts "found [lindex $lst $res] at index $res"
    
    set res [bisect $lst 0.9]
    puts "found [lindex $lst $res] at index $res"
    
    set res [bisect $lst 22.7]
    puts "found [lindex $lst $res] at index $res"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
I have thousands of HTML files to process using Groovy/Java and I need to
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.