for loop troubles

Jeff Shipman shippy at nmt.edu
Fri Apr 27 00:04:28 EDT 2001


I've used this method before to do funky for loop
stuff, but for some reason, I'm getting a line skipped
this time.

What I'm trying to do is convert items that have
one or more tabs at the beginning of a line followed
by a * into unordered list HTML. If I ever hit more
tabs than I'm currently at, I must be in a sublist
so I call this function recursively. It all works great
except when I come out of the recursive call, it
seems to skip the line right after (the first
previous-level element). Here's a code snippet
with the results

---
#!/usr/bin/python

import sys, re

def print_list(linelist, depth):
   if re.match('^\t+\*', linelist[0]):    # Unordered list
      level = len(re.match('^(\t+)\*', linelist[0]).group(1))
      print 3*depth*' '+'<UL>'
      for line in linelist:
         matchob = re.match('^(\t+)\*', line) 
         if matchob and len(matchob.group(1)) > level: # Uh oh, sublist!
            newlist = []
            j = linelist.index(line)
            while not matchob or (matchob and len(matchob.group(1))) >
level:
               newlist.append(linelist[j])
               linelist.pop(j)
               matchob = re.match('^(\t+)\*', linelist[j])
            print_list(newlist, depth+1)
            newlist = []
         elif not matchob:
            print 3*(depth+1)*' '+line,
         else:
            print 3*(depth+1)*' '+'<LI>'+line[depth+1:],
      print 3*depth*' '+'</UL>'

lines = [ '\t* First level list\n',
          '\t* Another element\n',
          '\t\t* A second level element\n',
          'Continuation of second level element\n',
          '\t* I\'ll never appear!\n',
          'Continuation if final element\n' ]

print_list(lines, 1)

--
This results in:
   <UL>
      <LI> First level list
      <LI> Another element
      <UL>
         <LI> A second level element
         Continuation of second level element
      </UL>
      Continuation if final element
   </UL>
--

Sure enough, the line that says it'll never appear
doesn't. I'm assuming it must have something to
do with my linelist.pop(j), but I do something similar
to this in several other locations in the program
and I've done it in other programs with no odd
problems. Basically, I'm doing that to get rid of
all the lines I'm passing to the recursive call.
It all seems to work great! Even if I have it
print out my linelist through every iteration,
I always see that mysterious line, but for some
reason it just gets skipped. I have no clue why.
If someone could please shed some light on my
problem with a possible fix or another way of
doing what I want to do, I'd greatly appreciate
it (like, a whole bunch!). I've been looking at
this for a couple hours now trying to figure out
what I'm doing wrong, and my eyes are feeling
heavy.....

Thanks in advance!

-- 
Jeff "Shippy" Shipman     E-Mail: shippy at nmt.edu
Computer Science Major    ICQ: 1786493
New Mexico Institute of Mining and Technology
Homepage: http://www.nmt.edu/~shippy



More information about the Python-list mailing list