I am really bored to convert the FORTRAN array decorator () to C array [], and eventually, I hacked this small script to do the tendious work:

#!/usr/bin/env python

import sys, re, string
p = re.compile( ‘([A-Z]+)\(([^\(\)]+)\) )
for line in sys.stdin:
    print string.rstrip( p.sub( r\1[\2]‘, line ))+";"

The usage is quite simple:

bookstack@tiger ~/work/bra_par $ ./main.py  < test
              RHOil=(DX[i-1]*RHO[ij]+DX[i]*RHO[il])/(DX[i]+DX[i-1]);
              RHOil=1.0/(RHOil+TINNY);
              RHOjt=(DY[j+1]*RHO[ij]+DY[j]*RHO[jt])/(DY[j]+DY[j+1]);
              RHOjt=1.0/(RHOjt+TINNY);

Here is the trick: the array consists variable name(captilized letters), (, whatever, ). we group the variable name and whatever as group 1, 2; then replace (, ) with [, ]. A simple demostration of how powerful the regular expression is.

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Leave a Comment