 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
damajha
Joined: 23 Oct 2006 Posts: 31
|
Posted: Tue Jan 08, 2008 7:11 am Post subject: Shifting the elements of an Array (EDIT: Post 4 +) |
|
|
Ok, I am missing something extreamely simple here.
All I want to do is take a list of names, and using StringSplit copy them into another list separated with a carriage return. This is what I came up with, but copying to the new variable in the loop isn't working:
| Code: | Players = Frank Bob Hank Bill Dale Tom
StringSplit, Order, Players, %A_Space%
loop, %Order0%
{
Names += Order%A_Index% 'n
} |
Arrgghh... It's always the simple things!! Maybe I'm just tired
Thanks in advance
Last edited by damajha on Wed Jan 09, 2008 4:29 am; edited 4 times in total |
|
| Back to top |
|
 |
DerRaphael
Joined: 23 Nov 2007 Posts: 456 Location: Heidelberg, Germany
|
Posted: Tue Jan 08, 2008 7:23 am Post subject: Re: StringSplit/Loop Prob |
|
|
| damajha wrote: | Ok, I am missing something extreamely simple here.
All I want to do is take a list of names, and using StringSplit copy them into another list separated with a carriage return. This is what I came up with, but copying to the new variable in the loop isn't working:
-------------------------------------------------------------
Players = Frank Bob Hank Bill Dale Tom
StringSplit, Order, Players, %A_Space%
loop, %Order0%
{
Names += Order%A_Index% 'n
}
-------------------------------------------------------------
Arrgghh... It's always the simple things!! Maybe I'm just tired
Thanks in advance |
use code-tags next time please
u missed the wrong operator should have been .= instead of +=
+= might be used when using numbers (integer, float point) and adding these. .= is used when putting strings (e.g. simple text, binary data) together
use it like this
| Code: |
StringSplit, Order, Players, %A_Space%
loop, %Order0%
{
Names .= Order%A_Index% 'n
}
|
greets
derRaphael _________________
|
|
| Back to top |
|
 |
damajha
Joined: 23 Oct 2006 Posts: 31
|
Posted: Tue Jan 08, 2008 7:38 am Post subject: |
|
|
Thanks,
It works untill I try to add the 'n! Then I get an Illegal character error.
| Code: |
Players = Frank Bob Hank Bill Dale Tom
StringSplit, Order, Players, %A_Space%
loop, %Order0%
{
Names .= Order%A_Index% `n
}
SplashTextOn, 300,300,,%Names%
Sleep, 2000
SplashTextOff
|
|
|
| Back to top |
|
 |
damajha
Joined: 23 Oct 2006 Posts: 31
|
Posted: Tue Jan 08, 2008 7:58 am Post subject: |
|
|
Maybe I'll expand on what I'm trying to do.
If I have a list of names (between 2 and 10)
Say:
Names = Joe Bob Hank Brian Elliot
I always want "Hank" to be the last name in the list, with everybody in the same order. In the above I just want the names shifted 2 to the right to be :
Names = Brian Elliot Joe Bob Hank.
Should be simple enough but I have been struggling for over an hour now!
Thanks |
|
| Back to top |
|
 |
damajha
Joined: 23 Oct 2006 Posts: 31
|
Posted: Tue Jan 08, 2008 8:32 am Post subject: |
|
|
lol... The `n needed to be in quotes!!
I'm an idiot!
Thanks |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2558 Location: Australia, Qld
|
Posted: Tue Jan 08, 2008 9:09 am Post subject: |
|
|
Why not
| Code: | | StringReplace, Names, Players, %A_Space%, `n, All |  |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1125
|
Posted: Tue Jan 08, 2008 10:31 am Post subject: |
|
|
| lexikos wrote: | Why not
| Code: | | StringReplace, Names, Players, %A_Space%, `n, All |  |
The Mad Scientist at work!
Then: | Code: | Players = [VxE] Frank Bob Hank Bill Dale Tom
Stringreplace, Players, Players, %a_space%Hank%a_space%, %a_space%, UseErrorLevel
If ErrorLevel
Players = %Players% Hank
StringReplace, Names, Players, %A_Space%, `n, All
MsgBox, %Names% |
always puts Hank at the end  _________________ My Home Thread
More Common Answers: 1. It's in the FAQ 2. Ternary ( ? : ) guide 3. Post code with [code][/code] tags |
|
| Back to top |
|
 |
damajha
Joined: 23 Oct 2006 Posts: 31
|
Posted: Wed Jan 09, 2008 4:22 am Post subject: |
|
|
| [VxE] wrote: | | lexikos wrote: | Why not
| Code: | | StringReplace, Names, Players, %A_Space%, `n, All |  |
The Mad Scientist at work!
Then: | Code: | Players = [VxE] Frank Bob Hank Bill Dale Tom
Stringreplace, Players, Players, %a_space%Hank%a_space%, %a_space%, UseErrorLevel
If ErrorLevel
Players = %Players% Hank
StringReplace, Names, Players, %A_Space%, `n, All
MsgBox, %Names% |
always puts Hank at the end  |
This puts Hank at the end, but it doesn't keep the names in the same order. In your example, I would want the list to read:
Bill Dale Tom [VXE] Frank Bob Hank
I have tried about 6 different ways and am missing something simple.
Thanks for the reply... All answers help with my understanding! |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2558 Location: Australia, Qld
|
Posted: Wed Jan 09, 2008 4:58 am Post subject: |
|
|
| damajha wrote: | | I always want "Hank" to be the last name in the list, with everybody in the same order. | Sorry, missed this post. Here are two different methods:
| Code: | Names = Joe Bob Hank Brian Elliot
Names := RegExReplace(Names, "(.*) Hank (.*)", "$2 $1 Hank")
StringReplace, Names, Names, %A_Space%, `n, All
MsgBox %Names%
|
| Code: | Names = Joe Bob Hank Brian Elliot
Names := SubStr(Names, (i:=InStr(Names, "Hank"))+5) " " SubStr(Names, 1, i-1) "Hank"
StringReplace, Names, Names, %A_Space%, `n, All
MsgBox %Names%
|
Regular expressions are generally considered an advanced topic, but I think in this case the regular expression is easier to follow. |
|
| Back to top |
|
 |
damajha
Joined: 23 Oct 2006 Posts: 31
|
Posted: Wed Jan 09, 2008 5:38 am Post subject: |
|
|
LOL.. Neither makes sense to me, but thank you!
I'm off to study! (At least I know what to read about!)
Thanks a tonne! |
|
| Back to top |
|
 |
DerRaphael
Joined: 23 Nov 2007 Posts: 456 Location: Heidelberg, Germany
|
Posted: Wed Jan 09, 2008 6:36 am Post subject: |
|
|
as a good entry for regex the help about regexreplace, regexmatch and regex overview are quite simple to read, also dont miss PhiLho's tutorial which explains some more advanced themes on http://phi.lho.free.fr/programming/RETutorial.en.html
greets
derRaphael _________________
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|