I was looking for ways to find the largest common substring between two given substrings and have learnt
1. .length is of type ulong
2. writing string[int] will not give me a sorted array
3. ulong cannot be sorted by sorted

What's the trick to sort the associative array by their keys?

Code snippet:
....

import std.stdio;

//find the largest substring of the two strings
void main(string[] args){

    string str1 = args[1];
    string str2 = args[2];

    string[ulong] commons;

    //is a char from str1[n] == str2[n]?
    //is the char from str1[n+1] == str2[n+1]?

    for (int i = 0; i < str1.length; i++){
        for (int j = 0; j < str2.length; j++){
            if (str2[j] == str1[i]){
                string current = ""~str1[i];
for (int a = 1; (a+i < str1.length && a+j < str2.length); a++){
                    if(str2[a+j] == str1[a+i]){
                        current ~= str1[a+i];
                    }
                }
                commons[current.length] = current;
            }
        }
    }
    writeln(commons); //[4:"asdf", 2:"df", 1:"f", 3:"sdf"]
//writeln(commons[commons.keys.max]); <-won't work, no max for ulong[]
    writeln(commons[commons.keys[0]]); //asdf
    //but can't guarantee the largest value is the first key
}

---
$./main asdf asdfasdfc
[4:"asdf", 2:"df", 1:"f", 3:"sdf"]


Reply via email to