# Conversion from a Php For loop to Xojo

Hello You all,
I used Xojo a long time ago when it stil was Real Basic and now come back after many years.
I have a hard time trying to convert a Php For loop to Xojo.
The trouble is this loop statement:

``````for (\$i = 0, \$l = \$this->leftmin, \$wl = mb_strlen(\$word); \$l < (\$wl - \$this->rightmin); \$l++) {
if (isset(\$array[\$l]) && \$array[\$l] % 2 !== 0) {
...
}
}
``````

My solution now is to have a for loop with an exit statement

``````l=LeftMin
for i=0 To Word.Length - RightMin
if LeftMin = Word.Length - RightMin then
exit
end
l=l+1
i=i+1
Next
``````

Would that work?
Could you point me in the right direction to help the If statement work. I canâ€™t find a solution. because Xojo doesnâ€™t seem to have an â€śissetâ€ť statement and I canâ€™t find a way to convert to Modulo statement
(The array is an array of strings)
Any help would be very much appreciated,
Thank you, Edward

What you are trying to achive looks faily simple. Maybe itâ€™s better to start from scratch.
What exactly are you trying to achive please?

BTW: The i=i+1 in Line 7 is not needed, because Xojo will automatically add 1 for each loop if you do not tell it to do something else. (Like with For X = 0 To 10 STEP 2.

``````// To make the Code work, we add the needed variables
Var Word As String = "sdfgijspgibgunsibg"
Var RightMin As Integer = 5
Var LeftMin As Integer = 3
Var l As Integer = LeftMin

For i As Integer = 0 To Word.Length - RightMin // = 13 in this example
If LeftMin = Word.Length - RightMin Then
Exit
End
l=l+1
Next

// In this example, l would just be counted fom LeftMin up to Word.Length - RightMin
// And it also adds +1 to l just before the Loop exits.
// This could be achived faster with the following code

If LeftMin <> Word.Length - RightMin Then
l = LeftMin + 1 + Word.Length - RightMin
Else
l = Word.Length - RightMin
End If
``````

(Edit: Corrected Posted wrong PHP Code)
Thank you! Indeed I forgot to remove the i=1+1
Iâ€™m trying to make an app that Hyphenates lyrics to enter in a music notation program. I found a php script that is usable, but I donâ€™t want to depend on a server to do the hard work. The actual php script has a function to hyphenat

``````private function hyphenateWord(\$word)
{
// is the word long enough?
if (mb_strlen(\$word) < \$this->wordmin) {
return \$word;
}
// does the word already contain the hyphen?
if (mb_strpos(\$word, \$this->hyphen) !== false) {
return \$word;
}
// WOORD TE LANG OF AL AFGEBROKEN

// prepare some variables
\$matchWord = '.' . \$word . '.';//Woord voorzien van punt voor en na het woord. Dus: .punt.
\$matchWordLength = mb_strlen(\$matchWord); // Lengte van het woord bepalen

\$matchWordChars = array();
for (\$i = 0; \$i < \$matchWordLength; \$i++) {
\$matchWordChars[] = mb_substr(\$matchWord, \$i, 1);
}// Woord naar een array converteren, MET PUNTEN
//echo var_export(\$matchWordChars);
\$matchWord = mb_strtolower(\$matchWord); //Hoofdletters uit woord halen

\$hyphenateWord = array(); // Array om het woord in op te slaan

// walk through the word and figure out, where to insert the hyphens
// Wandel door het woord
for (\$position = 0; \$position < (\$matchWordLength - \$this->charmin); \$position++) {
\$maxPatternLength = min(\$this->charmax, \$matchWordLength - \$position);

for (\$patternLength = \$this->charmin; \$patternLength < \$maxPatternLength; \$patternLength++) {

if (isset(\$this->pattern[mb_substr(\$matchWord, \$position, \$patternLength)])) {
\$pattern = \$this->pattern[mb_substr(\$matchWord, \$position, \$patternLength)];
\$digit = 1;

for (\$i = 0, \$l = mb_strlen(\$pattern); \$i < \$l; \$i++) {
\$char = \$pattern[\$i];

if (is_numeric(\$char) && \$char < 10) {
\$index = \$i === 0 ? \$position - 1 : \$position + \$i - \$digit;

if (!isset(\$hyphenateWord[\$index]) || \$hyphenateWord[\$index] !== \$char) {
\$hyphenateWord[\$index] = \$char;
}

\$digit++;
}
}
}
}
}
``````

But it depends heavily on Array lookups and â€śissetâ€ť checks. The For statements have more than once multiple start statements end conditional endings.
What I have now is this. I stored my Pattern in a dictionary that is a property of the Module where this is part off. CharMin/Max, RightMin, WordMin are also properties.

``````var HyphenateWord() As String
var Position As Integer =0
var PatternLength As Integer
var digit As Integer
var FoundCharacter As String
var num As Integer
var i,l As integer
var Index As integer

// Pattern check removed

//Variabelen voorbereiden
var MatchWord As String = "."+Word+"." //Woord inbedden tussen punten
var MatchWordLength As Integer = Word.length // Lengte van het woord vastleggen

var MatchWordChars() As String=MatchWord.ToArray //Woord inclusief punten opslaan in een array
//
MatchWord=MatchWord.Lowercase

//Wandel door het woord en bepaal de positie van de hyphens
// Regel 158 in phyph.php
for Position = 0 To MatchWordLength - CharMin
var MaxPatternLength as Double = min(CharMax, MatchWordLength - Position)

for PatternLength=Charmin To MaxPatternLength-1
var UsedPattern() As String = Pattern.Child(MatchWord.Middle(Position,PatternLength)).ToString.ToArray
digit=1
for i=0 To UsedPattern.Count
FoundCharacter = UsedPattern(i)
//Regel 169
If FoundCharacter.IsNumeric AND FoundCharacter.ToInteger<10 Then
if FoundCharacter.ToInteger = 0 Then
Index =  Position-1
else
Index = Position+i-Digit
end
end
if HyphenateWord(Index)<>FoundCharacter then
HyphenateWord(Index)=FoundCharacter
end
Digit=Digit+1
Next
end
Next
Next
// Hyphens invoegen
l=LeftMin
for i=0 To Word.Length - RightMin
if LeftMin = Word.Length - RightMin then
exit
end

l=l+1
Next
``````

Please excuse me, but english is not my first language and i struggle to fully understand.

Am i right that you want to split Lyrics by words or by Letters?
Like in this example?:

``````Var Word As String = "sd fgijsp gibgu nsibg"
Var SingleWords() As String = Word.Lowercase.Split() // String splitted into words, separated by " " (spaces).
Var SingleLetters() As String = Word.Lowercase.Split("") // String splitted into letters

// Var/Dim XYZ() = Defines a Variable and the () makes it an array of unknown dimensions. Write (5) for example if you want to make the array dimension 5.
// Lowercase = Converts the chars to be lowercase only
// Split = Splits strings by the provided delimiter (a SPACE if no delimiter is provided)
``````

Thank for your help. English isnâ€™t my native language either so I fully understand.
I try to integrate the â€śKnuth-Liangâ€ť algorithm that uses patterns and matches them against a given string to find places to hyphenate text. Enter hyphens at that place
So it changes:
â€śThis is an example to show abbreviationâ€ť to â€śThis is an example to show ab-bre-vi-a-tionâ€ť
Patterns are like

``````\$patterns=Array("_ach4,_ad4der, _af1t,...") //But they are converted into pairs before to a
// dictionary
``````

That sounds a lot more interesting, but itâ€™s beyond my abilities.
I could imagine various solutions using arrays and IndexOf, but to be honest I would have to experiment just like you.
I hope someone else can help you better from here. Iâ€™m sorry.

Just for fun, iâ€™ve asked ChatGPT to write some Code to have a starting Point. Hereâ€™s what it did come up with:

In a Module paste the following Function:

``````Public Function HyphenateText(Text As String) As String
Dim words() As String
Dim hyphenatedText As String
Dim word As String

words = Text.Split(" ")

For Each word In words
If word.Length > 3 Then
Dim half As Integer = word.Length \ 2
hyphenatedText = hyphenatedText + word.Left(half) + "-" + word.Middle(half) + " "
Else
hyphenatedText = hyphenatedText + word + " "
End If
Next

Return hyphenatedText.Trim
End Function
``````

And use it like this:

``````// Example usage:
Dim songText As String = "This is an example of song lyrics"
Dim hyphenatedSongText As String

hyphenatedSongText = HyphenateText(songText)
MessageBox(hyphenatedSongText)
``````

The result looks like this: â€śTh-is is an exa-mple of so-ng lyr-icsâ€ť

This code will split the input text into words, and if a word is longer than three characters, it will insert a hyphen in the middle of the word. The `HyphenateText` function returns the hyphenated text. Adjust the splitting and hyphenation logic as needed for your specific requirements.

A search on Github showed the Knuth-Liang algorithm in many different languages.
It might be easier to find another language than PHP to do the conversion as PHP and Xojo are very different.

I had a quick look at this one, written in Swift:

Just for fun, iâ€™ve asked ChatGPT to write some Code to have a starting Point. Hereâ€™s what it did come up with:
â€¦
The result looks like this: â€śTh-is is an exa-mple of so-ng lyr-icsâ€ť

This code will split the input text into words, and if a word is longer than three characters, it will insert a hyphen in the middle of the word.

Hmmâ€¦ I wished I was as simple as that.

1 Like

I guess you are right. Trouble is I donâ€™t â€śspeakâ€ť Swift and the code is split over several files. The one I used has itâ€™s code largely in one file so it was easier to follow.