Jump to content

Array to string


Recommended Posts

Hello. I got little problem which i couldnt resolve by myself. I had little array that i wanted to convert to string but i wanted delimter to be " \s " instead of " " or TAB. Couldn't get it to work, always was empty string. When i used it with " " worked fine. Thanks In advance

This works:

Dim $sArrayString = _ArrayToString( $array," ",1)
MsgBox( 4096, "_ArrayToString() Test", $sArrayString )

This doesn't:

Dim $sArrayString = _ArrayToString( $array," \s ",1)
MsgBox( 4096, "_ArrayToString() Test", $sArrayString )

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

Quote straight from help file

Remarks

This function is the opposite of the StringSplit function with the extension of being able to take a subset of all of the elements and placing just that subset into the string.

Since this function is the inverse of the StringSplit function, the delimiter can't be larger than 1 character in length.


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

  • Moderators

Bah, must have missed it when reading :/ Anyway, is there a way to go around this ?

Do you have to have \s as the delimeter?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Developers

Bah, must have missed it when reading :/ Anyway, is there a way to go around this ?

Func _ArrayToString(Const ByRef $avArray, $sDelim, $iStart = 0, $iEnd = 0)
; Declare local variables.
    Local $iCntr = 0, $iUBound = 0, $sResult = ""
    
; If $avArray is an array then set var for efficiency sake.
    If (IsArray($avArray)) Then
        $iUBound = UBound($avArray) - 1
    EndIf
    If $iEnd = 0 Then $iEnd = $iUBound
; Check for parameter validity.
    Select
        Case (Not IsArray($avArray))
            SetError(1)
            Return ""
        Case( ($iUBound + 1) < 2 Or UBound($avArray, 0) > 1)
            SetError(2)
            Return ""
        Case (Not IsInt($iStart))
            SetError(3)
            Return ""
        Case (Not IsInt($iEnd))
            SetError(5)
            Return ""
        Case (Not IsString($sDelim))
            SetError(7)
            Return ""
        Case ($sDelim = "")
            SetError(8)
            Return ""
    ;Case (StringLen($sDelim) > 1)
    ;   SetError(9)
    ;   Return ""
        Case ($iStart = -1 And $iEnd = -1)
            $iStart = 0
            $iEnd = $iUBound
        Case ($iStart < 0)
            SetError(4)
            Return ""
        Case ($iEnd < 0)
            SetError(6)
            Return ""
    EndSelect
    
; Make sure that $iEnd <= to the size of the array.
    If ($iEnd > $iUBound) Then
        $iEnd = $iUBound
    EndIf
    
; Combine the elements into the string.
    For $iCntr = $iStart To $iEnd
        $sResult = $sResult & $avArray[$iCntr]
        If ($iCntr < $iEnd) Then
            $sResult = $sResult & $sDelim
        EndIf
    Next
    
    SetError(0)
    Return $sResult
EndFunc  ;==>_ArrayToString

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Bah, must have missed it when reading :/ Anyway, is there a way to go around this ?

easiest way would probably be a quick 'for' loop through the array that concatenates a "\" to the end of each element (except the 0 element if that's your count), then use "s" as your delimiter...
Link to comment
Share on other sites

@JDEB - a lot of code for small proggie like mine ;)

@SMOKE - well i have to use /s as SecureCRT.EXE command line uses /s as session trigger so i need to use it like SecureCRT.EXE /s session1 /s session2 /session3 and as it takes the sessions names from .ini file:

secure_sessions_names i had to 'divide' it somehow :o I solved it by doing 10 If's but that made it a bit long

and i can't set more then 10 sessions (not like i will but for better use :geek:

If FileExists($secure_exe) Then
     If $array[0] = 1 Then Run($secure_exe & " /s " & $array[1] )
     If $array[0] = 2 Then Run($secure_exe & " /s " & $array[1] & " /s " & $array[2])

and so on up to 10.

@cameronsdad - been going thru my mind to make all kind of loops :sorcerer: well it's not a biggy anyway atm.

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

@JDEB - a lot of code for small proggie like mine ;)

@SMOKE - well i have to use /s as SecureCRT.EXE command line uses /s as session trigger so i need to use it like SecureCRT.EXE /s session1 /s session2 /session3 and as it takes the sessions names from .ini file:

secure_sessions_names i had to 'divide' it somehow :o I solved it by doing 10 If's but that made it a bit long

and i can't set more then 10 sessions (not like i will but for better use :geek:

If FileExists($secure_exe) Then
     If $array[0] = 1 Then Run($secure_exe & " /s " & $array[1] )
     If $array[0] = 2 Then Run($secure_exe & " /s " & $array[1] & " /s " & $array[2])

and so on up to 10.

@cameronsdad - been going thru my mind to make all kind of loops :sorcerer: well it's not a biggy anyway atm.

For $x = 0 to Ubound($array)
$array[$x] = $array[$x] & "/"
Next
$newstring = _ArrayToString($array,"s")

or if you want to have it like:

element /s element

then it would just be:

For $x = 0 to Ubound($array)
$array[$x] = " " & $array[$x] & " /"
Next
$newstring = _ArrayToString($array,"s")
Link to comment
Share on other sites

  • Developers

@JDEB - a lot of code for small proggie like mine :geek:

Nope... its the original UDF with the lines commented that are giving the limitation of one character... :o

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Nope... its the original UDF with the lines commented that are giving the limitation of one character... :o

@JDEB - i am not yet at that stage to fully understand UDF code ;p So i took it as it was solution to my problem :geek:

@CameronSDAD tnx

My little company: Evotec (PL version: Evotec)

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...