Jump to content

String Concatenation


Recommended Posts

Hello all!

At first, I'm sorry of my English.

I have an array of strings, e.g.

$Array[0]="X"
$Array[1]="Y"
$Array[2]="Z"

And I should make new array of all strings which may concate from this.

Something like:

$Array[0]="XY"
$Array[1]="XZ"
$Array[2]="YX"
$Array[3]="YZ"
$Array[4]="ZX"
$Array[5]="ZY"
$Array[6]="XYZ"
$Array[7]="XZY"
...
$Array[Last]="ZYX"

Have anyone an idea of how to do that?

Hello World!

Link to comment
Share on other sites

Hello all!

At first, I'm sorry of my English.

I have an array of strings, e.g.

$Array[0]="X"
$Array[1]="Y"
$Array[2]="Z"

And I should make new array of all strings which may concate from this.

Something like:

$Array[0]="XY"
$Array[1]="XZ"
$Array[2]="YX"
$Array[3]="YZ"
$Array[4]="ZX"
$Array[5]="ZY"
$Array[6]="XYZ"
$Array[7]="XZY"
...
$Array[Last]="ZYX"

Have anyone an idea of how to do that?

:EDIT:

Oops, mis-read...

I've written something that does this... Let me see if I can find it.

Edited by Szhlopp
Link to comment
Share on other sites

OK. =)

Here is the more detailed (I think) description of what I want:

From first array I must get the second. Substrings from the second one shouldn't repeat.

Edited by Crystall

Hello World!

Link to comment
Share on other sites

Thanks Thanubis, this should be useful for me.

But maybe anyone may make an example script, which will do what I describe in first post?

Because of night time, I going sleep. :)

Maybe at morning I will join _ArrayCombinations() and _ArrayPermute() and get correct solution =)

Edited by Crystall

Hello World!

Link to comment
Share on other sites

This works only for your example and not for something like "W", "X", "Y", "Z" or just "X", "Y". In short, it can't process arbitrary array length:

#include <Array.au3>

Dim $avArr1[3] = ["X", "Y", "Z"]
Dim $avArr2
Dim $sBuff = ''

For $i = 0 To UBound($avArr1)-2
    For $j = 0 To UBound($avArr1)-1
        $sBuff &= _RecursiveDownward(UBound($avArr1)-1-$i) & "|" & _RecursiveUpward($i) & "|"
        For $k = 1 To UBound($avArr1)-1
            $sTmp = $avArr1[$k-1]
            $avArr1[$k-1] = $avArr1[$k]
            $avArr1[$k] = $sTmp
        Next
    Next
Next

$avArr2 = StringSplit(StringTrimRight($sBuff, 1), "|", 2)
_ArraySort($avArr2)
_ArrayDisplay($avArr2)

Func _RecursiveUpward($iDepth)
    If $iDepth < UBound($avArr1)-1 Then
        Return $avArr1[$iDepth] & _RecursiveUpward($iDepth+1)
    Else
        Return $avArr1[$iDepth]
    EndIf
EndFunc

Func _RecursiveDownward($iDepth)
    If $iDepth > 0 Then
        Return $avArr1[$iDepth] & _RecursiveDownward($iDepth-1)
    Else
        Return $avArr1[$iDepth]
    EndIf
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...