#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155

    My Version of UBound


    I needed to create a Python version of the Visual Basic 6 function UBound(). But I needed a generic version. One that would return the largest integer (or float, etc.) in a list. Here is the code, below is the error I receive in which I'm not sure how to fix.
    Code:
    def pyUBound(dimension):
        it = iter(dimension); it.next()
        for i in dimension:
            while(1):
                if(i > dimension[it.next()]):
                    cHigh = i; continue
                    while(1):
                        if(cHigh > dimension[it.next()]):
                            cHigh = i; continue
                        else:
                            break
                else:
                    cHigh = None
                    continue
        return cHigh
    Code:
        if(i > dimension[it.next()]):
    StopIteration
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    UBound returns the largest array index, not the largest element in the array. With that said, you can easily implement UBound using the len() function.
    Code:
    #!/usr/bin/env python
    
    def UBound(dim):
        try:
            return len(dim) - 1
        except TypeError:
            return 0
    
    foo = 1
    print UBound(foo)
    bar = [2, 3, 5, 7]
    print UBound(bar)
    print 'last element of bar is', bar[UBound(bar)]
    baz = (1, 7, 5, 2)
    print UBound(baz)
    print 'last element of baz is', baz[UBound(baz)]
    quux = "123456"
    print UBound(quux)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Oh, I guess I read MSDN wrong. Thanks for the help Scorpions.

IMN logo majestic logo threadwatch logo seochat tools logo