def remove_arrays(code, count=1):
 res = ''
 last = ''
 replacements = {}
 for e in bracket_split(code, ['[]']):
  if (e[0] == '['):
   if is_array(last):
    name = (ARRAY_LVAL % count)
    res += (' ' + name)
    replacements[name] = e
    count += 1
   else:
    (cand, new_replacements, count) = remove_arrays(e[1:(-1)], count)
    res += ('[%s]' % cand)
    replacements.update(new_replacements)
  else:
   res += e
  last = e
 return (res, replacements, count)

def remove_arrays ( code , count = 1 ) : res = '' last = '' replacements = { } for e in bracket_split ( code , [ '[]' ] ) : if ( e [ 0 ] == '[' ) : if is_array ( last ) : name = ( ARRAY_LVAL % count ) res += ( ' ' + name ) replacements [ name ] = e count += 1 else : ( cand , new_replacements , count ) = remove_arrays ( e [ 1 : ( - 1 ) ] , count ) res += ( '[%s]' % cand ) replacements . update ( new_replacements ) else : res += e last = e return ( res , replacements , count )
removes arrays and replaces them with array_lvals returns new code and replacement dict *note* has to be called after remove objects .