[Tutor] arrays

federico ramirez fedekiller at gmail.com
Fri Sep 15 02:11:24 CEST 2006


Hi all! Im started with python some days ago, im trying to make a basic cgi
script, with dbm, and dbm returns a dictionary witout order, so i put it in
an array to order the keys and then display it in order but..python orders
the array like this

['_1', '_10', '_11', '_12', '_13', '_2', '_3', '_4', '_5', '_6', '_7', '_8',
'_9']

and i want

['_1', '_2', '_3', '_4', '_5', '_6', '_7', '_8', '_9','_10', '_11', '_12',
'_13']

here its the code i use

///////////////////////////////

#!/usr/bin/python

print "Content-Type: text/html\n\n"

import cgi, dbm

def startpage(title):
    print '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
    <title>''',title,'''</title>
    </head>
    <body>'''

def endpage():
    print """</body>
    </html>"""

def main():
    form = cgi.FieldStorage()
    if(form.has_key("name") and form["name"].value != ""):
        db = dbm.open("dbm", "c")
        db.close()
        dbread = dbm.open("dbm", "r")
        leng = len(dbread)+1
        dbread.close()
        db = dbm.open("dbm", "c")
        name = "_"+str(leng)
        db[name] = form["name"].value
        db.close()
        print "Info saved. Now, <a href='?read=true'>read</a> it?"
    elif(form.has_key("read") and form["read"].value == "true"):
        db = dbm.open("dbm", "r")
        arr = []
        for key in db.keys():
            arr += [key]
        arr.sort()
        arr.reverse()
        for i in range(len(db)):
            print db[arr[i]],'<br />'
        db.close()
    else:
        print """<form action="test.py" method="post">
        <input name="name" type="text" id="name" />
        <input type="submit" name="Submit" value="Send my name" />
        </form>"""

startpage("Test Page")
main()
endpage()

//////////////////////////////

Thanks in advanced

-- 
Best Regards.
fedekiller
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060914/40104f4c/attachment.htm 


More information about the Tutor mailing list