% Parsing function to extract the first element of the 
% Lie bracket and calculate its ordering position.
% e.g. x = [Bi,Bj] -> first element is Bi.
% The Philip Hall basis constructed so far is pased as
% the variable 'list' and the position of Bi in 'list'
% is returned as 'op', the ordering position.
% Miguel Torres-Torriti (c) 7.MAR.2001
%
function op=orderpos(x,list)

if isempty(findstr(char(x),'['))
   op=sscanf(char(x),'%d');
  else 
   c=findstr(char(x),'].[');
   if isempty(c) % Parsing case: *.[* or case *.*
      %op=sscanf(char(x),'%*c%d',1); %Win version (For some reason under
				     %Windows sscanf behaves differently).
      aux=sscanf(char(x),'%*c%d',2); %Unix version (requires this two lines
      op=aux(1);                     %Unix        instead of the Win version).
   else
      s=findstr(char(x),'.[');
      sm=min(s);
      cm=min(c);
      if sm<cm %Parsing case: *.[[*]].[*
         op=sscanf(char(x),'%*c%d',1);
      else  % Parsing case: *].[*
         cb=1;
         aux=cm-1;
         while (x(aux)==']') % Count closing brackets
            cb=cb+1;
            aux=aux-1;
         end
         ob=1; 
         aux=2;
         while (x(aux)=='[') % Count openning brackets
            ob=ob+1;
            aux=aux+1;
         end
         
         op=1;
         while (strcmp(x(ob-cb+1:cm),list(op))==0),
            op=op+1;
         end
      end
   end
end
   
