Fixing MDX using F#

Something like this could be useful if you have a large MDX script and want to translate some codes from one set to another set. (Perhaps I should find a more compact representation for the code translation table, like JSON, or even just a delimited string. I miss Perl’s quoting and literal syntaxes, sometimes…)

A script I just wrote; slight changes to the “pat” to make it less specific to the cube I’m working on. Edit as necessary.

open System
open System.IO
open System.Text.RegularExpressions
let filename = @"c:\temp\mdxscript.txt"
let txt = File.ReadAllText filename

let pat = "(\[yourDimensionName\]\.\[yourHierarchyName\]\.&\[)(\d+)(\])"
let oldToSnomed = 
    Map.ofList
      [ "oldcode1", "snowmedcode1";
        "oldcode2", "snowmedcode2"]
let replacer (m:Match) = 
    let orig   = m.Captures.[0].Value 
    let before = m.Groups.[1].Value
    let code   = m.Groups.[2].Value
    let after  = m.Groups.[3].Value
    printfn "%s" code
    let converted = oldToSnomed.TryFind(code)
    match converted with 
        | Some snomed -> before + snomed + after
        | _ -> orig

let evaluator = new MatchEvaluator(replacer)

let txt2 = Regex.Replace(txt, pat, evaluator)

txt2

File.WriteAllText(filename, txt2) // overwrites with translated codes
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s