FlipString Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

FlipString

24 Mar 2017, 05:00

Hi!

Found one of lexikos Flip(Str) functions and it does truly work nicely. It does what it is supposed to do, but when the string contains a two digit value (10) these digits are flipped too (01), which breaks the handling in my script. How would one go about to prevent that to happen and still flip the string upside down, resulting in (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)? This should also apply to three digit values.

Code: Select all

FlipStr := "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
msgbox % Flip(FlipStr) ; Reverse the string order.
Flip(in) {
    VarSetCapacity(out, n:=StrLen(in))
    Loop %n%
        out .= SubStr(in, n--, 1)
    return out
}
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: FlipString

24 Mar 2017, 05:14

You can use StrSplit with , and reverse the array or if its possible use an array and not a string to store such informations.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Re: FlipString

24 Mar 2017, 05:49

Im using an array to store the information in my big script, but do parse it to a string to pass it to Flip(Str), cuz there are no examples I can find, when searching, how to reverse an array easily. Using StringSplit (which also is kind of an array) gets unmanageble quite fast if trying to reverse the stringsplit outcome order, especially when the splits are as many as three digits.
But I will try sorting the array retrievals and then put them inside a new array.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: FlipString

24 Mar 2017, 06:19

Try this:

Code: Select all

FlipStr := "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
msgbox % Flip(FlipStr)

Flip(List) {
    Sort, List, RND`,
    Return, List
}
or this:

Code: Select all

FlipStr := "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
Sort, FlipStr, RND`,
msgbox %FlipStr%
I hope that helps.
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Re: FlipString

24 Mar 2017, 06:22

You did beat me by a minute :D
Thanks!
This was my attempt (not finished though):

Code: Select all

FlipStr := [1,2,3,4,5,6,7,8,9,10]
ReverseArray := []

for k, v in FlipStr {
  flipstrings .= (A_Index > 1 ? "`n" : "") v 
}
Sort, flipstrings, F ReverseDirection  ; Reverses the list so that it contains 4,3,2,1
Loop, Parse, flipstrings, `n
{
  ReverseArray.Push(A_LoopField)
}
for k, v in ReverseArray 
 reversedflipstrings .= v "`n"
msgbox % reversedflipstrings

ReverseDirection(a1, a2, offset)
{
    return offset  ; Offset is positive if a2 came after a1 in the original list; negative otherwise.
}
EDIT..Now its cleaned up :)
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: FlipString

24 Mar 2017, 06:34

Code: Select all

OldArr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i, v in OldArr
    MsgBox % v


NewArr := ReverseArray(OldArr)

for i, v in NewArr
    MsgBox % v
    


ReverseArray(arr)
{
    NewArr := []
    loop % len := arr.MaxIndex()
        NewArr[len - (A_Index - 1)] := arr[A_Index]  ; or -> NewArr[A_Index] := arr[len - (A_Index - 1)]
    return NewArr
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Re: FlipString

24 Mar 2017, 06:39

OMG :shock:
Now that was a lesson in higher class :D
Much obliged jNizM :)

Regards
zcooler
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: FlipString  Topic is solved

24 Mar 2017, 06:47

Or alternative with byref and change the existing array without creating another one

Code: Select all

Arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i, v in Arr
    MsgBox % v

ReverseArray(Arr)

for i, v in Arr
    MsgBox % v

ReverseArray(ByRef arr)
{
    loop % len := arr.MaxIndex()
        arr.Push(arr.RemoveAt(len - (A_Index - 1)))
    return
}
edit / changed:
ObjInsert -> Pop
ObjRemove -> RemoveAt
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: FlipString

24 Mar 2017, 06:50

maybe this:

Code: Select all

Reverse(Arr) {
    NewArr := []
    For each, Key in Arr
        NewArr.InsertAt(1, Key)
    Return, NewArr
}
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Re: FlipString

24 Mar 2017, 06:56

In the name of the truth, far out that was some brilliant stuff :mrgreen: The second jNizMs function is the most juicy stuff :clap:
THANKS to all of you guys and especially jNizM :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, justcop, Rohwedder and 120 guests