AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Shifting the elements of an Array (EDIT: Post 4 +)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
damajha



Joined: 23 Oct 2006
Posts: 31

PostPosted: Tue Jan 08, 2008 7:11 am    Post subject: Shifting the elements of an Array (EDIT: Post 4 +) Reply with quote

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
View user's profile Send private message
DerRaphael



Joined: 23 Nov 2007
Posts: 456
Location: Heidelberg, Germany

PostPosted: Tue Jan 08, 2008 7:23 am    Post subject: Re: StringSplit/Loop Prob Reply with quote

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
View user's profile Send private message
damajha



Joined: 23 Oct 2006
Posts: 31

PostPosted: Tue Jan 08, 2008 7:38 am    Post subject: Reply with quote

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
View user's profile Send private message
damajha



Joined: 23 Oct 2006
Posts: 31

PostPosted: Tue Jan 08, 2008 7:58 am    Post subject: Reply with quote

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
View user's profile Send private message
damajha



Joined: 23 Oct 2006
Posts: 31

PostPosted: Tue Jan 08, 2008 8:32 am    Post subject: Reply with quote

lol... The `n needed to be in quotes!!

I'm an idiot!

Thanks
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2558
Location: Australia, Qld

PostPosted: Tue Jan 08, 2008 9:09 am    Post subject: Reply with quote

Why not
Code:
StringReplace, Names, Players, %A_Space%, `n, All
Question
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 1125

PostPosted: Tue Jan 08, 2008 10:31 am    Post subject: Reply with quote

lexikos wrote:
Why not
Code:
StringReplace, Names, Players, %A_Space%, `n, All
Question

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 Crying or Very sad
_________________
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
View user's profile Send private message
damajha



Joined: 23 Oct 2006
Posts: 31

PostPosted: Wed Jan 09, 2008 4:22 am    Post subject: Reply with quote

[VxE] wrote:
lexikos wrote:
Why not
Code:
StringReplace, Names, Players, %A_Space%, `n, All
Question

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 Crying or Very sad


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
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2558
Location: Australia, Qld

PostPosted: Wed Jan 09, 2008 4:58 am    Post subject: Reply with quote

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
View user's profile Send private message
damajha



Joined: 23 Oct 2006
Posts: 31

PostPosted: Wed Jan 09, 2008 5:38 am    Post subject: Reply with quote

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
View user's profile Send private message
DerRaphael



Joined: 23 Nov 2007
Posts: 456
Location: Heidelberg, Germany

PostPosted: Wed Jan 09, 2008 6:36 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group