1

How can I check in LibreOffice Calc whether two words have the same letters, i. e. are anagrams? Is this doable with the built-in functions?

Edit: My idea is: convert the letters in both words to lower case, sort the letters alphabetically and compare the results. With Python this is easy:

a1 = "Anagram"
b1 = "nagaram"

print("a1 & b1 are anagrams: ", "".join(sorted(a1.lower())) == "".join(sorted(b1.lower())))

But how can I do this with LibreOffice Calc? The function SORT() needs a range or an array. But how can I split a cell value into an array?

At first this seemed to be such a simple task...

4
  • What do you call "built-in functions"? Do you mean the function to use in formulas? Or do you mean functions to use in macros? I'd only see the latter as "programming" to make this question on-topic. Commented Nov 28, 2024 at 7:52
  • What does your research in the documentation reveal? Please edit your question to clarify, and tell us why your findings did not find help you. Do you have an algorithm in mind? If so, please add that, too. If not, please edit your question accordingly. Commented Nov 28, 2024 at 7:54
  • did you look at split and join functions? Commented Nov 30, 2024 at 21:21
  • @njzk2 Well I looked for a split function, but didn't find one. I experimented with MID() in a temporary table, but it got really ugly ... with no usable result. Commented Dec 1, 2024 at 11:44

2 Answers 2

1

So it seems you can't do it with the built-in functions, but as described here, you can create user-defined functions. So I created two functions:

REM  *****  BASIC  *****

Function SortLetters(a AS String, Optional toLowerCase As Boolean) As String
    GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")
    Dim svc : svc = CreateScriptService("Array")
    
    if IsMissing(toLowerCase) then
        toLowerCase = true
    end if
    
    if toLowerCase then
        a = LCase(a)
    end if
    
    Dim letters(Len(a))
    for counter=0 to Len(a)
        letters(counter) = Mid(a, counter+1, 1)
    next
    SortLetters = Join(svc.Sort(letters), "")
    
    svc.Dispose()
End Function

Function CheckAnagram(a as String, b as String) as Boolean
    CheckAnagram = SortLetters(a, true) = SortLetters(b, true)
End Function

You can use these functions in a cell with =SortLetters(A1)=SortLetters(B1) or =CheckAnagram(A1;B1). Now it would be really nice if you could create a help for the usage. But an addin would really be too much effort for this little experiment.

Sign up to request clarification or add additional context in comments.

Comments

0

You'll need a macro to do this, but basically you split each word into letters, sort the letters into alphabetical order, and then check if the results match. I have C code to do this which you can find here, but translating it into a LibreOffice Basic macro would require work. I think LibreOffice now supports Python macros, and it might be easier to translate C into Python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.