Ughm help

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Ughm help

11 Apr 2018, 07:50

Can somebody help me fix this please? I donno why it's not working!

Code: Select all

Loop % People.MaxIndex() {
    RegExMatch(People[A_Index].Status, "\(\d+/", People[A_Index].Value)
    StringTrimLeft, People[A_Index].Value, People[A_Index].Value, 1
    StringTrimLeft, People[A_Index].Value, People[A_Index].Value, 1
}
^^ code to be fixed

(EDIT:) RegEx does work outside the loop. I want the value between ( and / to be stored in another array. People[Number].Value

Full Code (is proper):

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)

Array := StrSplit(TestTexti, "`n")

splitArray := StrSplit(TestTexti, "`n")

People := {}

Loop, % splitArray.MaxIndex() {

	if (Mod(A_Index, 2) == 0) {
		continue
	}

	person := {}
	person.Name := splitArray[A_Index]
	person.status := splitArray[A_Index + 1]

	People.push(person)
}
I am your average ahk newbie. Just.. a tat more cute. ;)
somhairle
Posts: 11
Joined: 09 Apr 2018, 12:05

Re: Ughm help

11 Apr 2018, 10:48

On your full code:
1) Why are you splitting the string twice?
2) You can simplify your code with a call to Object().
See comments below.

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)
; This line is unnecessary
; Array := StrSplit(TestTexti, "`n")

splitArray := StrSplit(TestTexti, "`n")
; Replace this...
;People := {}

;Loop, % splitArray.MaxIndex() {

;	if (Mod(A_Index, 2) == 0) {
;		continue
;	}

;	person := {}
;	person.Name := splitArray[A_Index]
;	person.status := splitArray[A_Index + 1]

;	People.push(person)

;}
; ...with this.
People := Object(splitArray*)
For your RegEx:
1) Use a for loop.
2) You can capture subpatterns in your RegEx so don't need the extra trim steps - note the extra parenthesis.

Code: Select all

for person, status in People {
    RegExMatch(status, "O)\((\d+)/", output)
    MsgBox % output.Value[1]
}
But you don't really need all that extra since match will return the first match:

Code: Select all

for person, status in People {
    RegExMatch(status, "\d+", output)
    MsgBox % output
}
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Ughm help

11 Apr 2018, 11:06

somhairle wrote:On your full code:
1) Why are you splitting the string twice?
2) You can simplify your code with a call to Object().
See comments below.

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)
; This line is unnecessary
; Array := StrSplit(TestTexti, "`n")

splitArray := StrSplit(TestTexti, "`n")
; Replace this...
;People := {}

;Loop, % splitArray.MaxIndex() {

;	if (Mod(A_Index, 2) == 0) {
;		continue
;	}

;	person := {}
;	person.Name := splitArray[A_Index]
;	person.status := splitArray[A_Index + 1]

;	People.push(person)

;}
; ...with this.
People := Object(splitArray*)
For your RegEx:
1) Use a for loop.
2) You can capture subpatterns in your RegEx so don't need the extra trim steps - note the extra parenthesis.

Code: Select all

for person, status in People {
    RegExMatch(status, "O)\((\d+)/", output)
    MsgBox % output.Value[1]
}
But you don't really need all that extra since match will return the first match:

Code: Select all

for person, status in People {
    RegExMatch(status, "\d+", output)
    MsgBox % output
}
Really appreciate it!

But I want to store the names and status separately. And the values should correspond to this and have separate variables.. So I can call Value1 or Value2 anytime..

Well functions are my grey area. @swagfag helped me make that in this thread: https://autohotkey.com/boards/viewtopic.php?f=5&t=47045
I go blind when it comes to objects so haha maybe one day I will look back at me and say that I was stupid. Well that's for then I guess. :lol:
Last edited by Nwb on 11 Apr 2018, 11:24, edited 4 times in total.
I am your average ahk newbie. Just.. a tat more cute. ;)
somhairle
Posts: 11
Joined: 09 Apr 2018, 12:05

Re: Ughm help

11 Apr 2018, 11:17

Nothing wrong with the original function. The replacement I suggested isn't well documented and it helps to have a little knowledge in other languages to dig it up. If you need (relatively) simple explanations on anything I suggested, feel free to ask.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Ughm help

11 Apr 2018, 11:25

somhairle wrote:Nothing wrong with the original function. The replacement I suggested isn't well documented and it helps to have a little knowledge in other languages to dig it up. If you need (relatively) simple explanations on anything I suggested, feel free to ask.
How do I get Name1 when I want it? And Status1 and Value1?
Like what is it stored as? Person[1]?

I am really bad at objects. :?

I want there to be output1 output2 output3 and so on until there are corresponding Status. I'm sorry if I'm being to vague or hard to understand..
I am your average ahk newbie. Just.. a tat more cute. ;)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Ughm help

11 Apr 2018, 12:05

presumably you actually only want to extract a status' number and not the entirety of "Status - (6/10)", so why not do just that in the same step youre initializing your collection of people

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)

splitArray := StrSplit(TestTexti, "`n")

People := {}

Loop, % splitArray.MaxIndex() {

	if (Mod(A_Index, 2) == 0) {
		continue
	}

	person := {}
	person.name := splitArray[A_Index]

	unprocessedStatus := splitArray[A_Index + 1]
	RegExMatch(unprocessedStatus, "\d+", statusScore)
	person.status := statusScore

	People.push(person)
}

for index, person in People {
	MsgBox, % "index: " . index 
		. "`nName: " . person.name
		. "`nStatus: " . person.status
}
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Ughm help

11 Apr 2018, 12:18

swagfag wrote:presumably you actually only want to extract a status' number and not the entirety of "Status - (6/10)", so why not do just that in the same step youre initializing your collection of people

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)

splitArray := StrSplit(TestTexti, "`n")

People := {}

Loop, % splitArray.MaxIndex() {

	if (Mod(A_Index, 2) == 0) {
		continue
	}

	person := {}
	person.name := splitArray[A_Index]

	unprocessedStatus := splitArray[A_Index + 1]
	RegExMatch(unprocessedStatus, "\d+", statusScore)
	person.status := statusScore

	People.push(person)
}

for index, person in People {
	MsgBox, % "index: " . index 
		. "`nName: " . person.name
		. "`nStatus: " . person.status
}
I want both status and value. :P I appreciate it.
I know it might be simple substitution but I'm not that good with objects at the moment.
I am your average ahk newbie. Just.. a tat more cute. ;)
somhairle
Posts: 11
Joined: 09 Apr 2018, 12:05

Re: Ughm help

11 Apr 2018, 12:22

If you just want the name then, for example, you would just do People[1].name. Likewise for status - People[1].status.

Edit: Based on swagfag's code, not mine.
Last edited by somhairle on 11 Apr 2018, 12:24, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Ughm help

11 Apr 2018, 12:23

great, then just create another property

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)

splitArray := StrSplit(TestTexti, "`n")

People := {}

Loop, % splitArray.MaxIndex() {

	if (Mod(A_Index, 2) == 0) {
		continue
	}

	person := {}
	person.name := splitArray[A_Index]
	person.status := splitArray[A_Index + 1]

	RegExMatch(person.status, "\d+", score)
	person.score := score

	People.push(person)
}

for index, person in People {
	MsgBox, % "index: " . index 
		. "`nName: " . person.name
		. "`nStatus: " . person.status
		. "`nScore: " . person.score
}
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Ughm help

11 Apr 2018, 13:06

swagfag wrote:great, then just create another property

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)

splitArray := StrSplit(TestTexti, "`n")

People := {}

Loop, % splitArray.MaxIndex() {

	if (Mod(A_Index, 2) == 0) {
		continue
	}

	person := {}
	person.name := splitArray[A_Index]
	person.status := splitArray[A_Index + 1]

	RegExMatch(person.status, "\d+", score)
	person.score := score

	People.push(person)
}

for index, person in People {
	MsgBox, % "index: " . index 
		. "`nName: " . person.name
		. "`nStatus: " . person.status
		. "`nScore: " . person.score
}
You're awesome.. thanks a lot. :superhappy:

Can you help me with this though:

Code: Select all

Loop % Person.MaxIndex() {
	PersonNamePop%A_Index% := person[A_Index].name 
	%PersonNamePop%A_Index%% := 1
}
Basically I want the person's name to become a separate variable and be equal to 1. But donno how to do that.. :crazy:
This is useful to see who's values have not been registered for a pending list. All names are equal to 0 at the beginning so if it hasn't been changed to 1 then it's pending, that's the idea.
I am your average ahk newbie. Just.. a tat more cute. ;)
somhairle
Posts: 11
Joined: 09 Apr 2018, 12:05

Re: Ughm help

11 Apr 2018, 13:20

Like this?

Code: Select all

for index, person in People {
    name := person.name
    %name% := 1
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Ughm help

11 Apr 2018, 13:32

no, dont do this endless-variable-creation stuff

just define another property

Code: Select all

person.isRegistered := false
somhairle
Posts: 11
Joined: 09 Apr 2018, 12:05

Re: Ughm help

11 Apr 2018, 13:36

^^^Much better -- dynamic variables are a no-no^^^
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Ughm help

12 Apr 2018, 03:22

swagfag wrote:no, dont do this endless-variable-creation stuff

just define another property

Code: Select all

person.isRegistered := false
Ughm I don't understand.

I need to write the people who are pending on a separate .txt


So here's the people's information that I did get:

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)
So I get person[1].Name, person[2].Name, person[3].Name

So the values of person[1].Name and etc should not be put in the pending list. The values contain the names.

There are some other people
Name4
Name5

that didn't get registered. So they should be in the pending list.


I thought I would do this:
Literal Name1 := 0
Literal Name2 := 0
Literal Name3 := 0
Literal Name4 := 0
Literal Name5 := 0

And then loop to set literal names to := 1. (it says variable contains invalid so I am clueless how to do this)

And when writing a Pending List,
If Literl Name1 := 0
FileAppend.


There are a totla of 15 Names.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Ughm help

12 Apr 2018, 03:31

somhairle wrote:Like this?

Code: Select all

for index, person in People {
    name := person.name
    %name% := 1
}
When I do this it says %name% contains invalid character. But the Names only are letters and occasionally numbers. Is it because it has blankspace?

I'm using this script

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)

splitArray := StrSplit(TestTexti, "`n")

People := {}

Loop, % splitArray.MaxIndex() {

	if (Mod(A_Index, 2) == 0) {
		continue
	}

	person := {}
	person.name := splitArray[A_Index]
	person.status := splitArray[A_Index + 1]

	RegExMatch(person.status, "\d+", score)
	person.score := score

	People.push(person)
}

for index, person in People {
	MsgBox, % "index: " . index 
		. "`nName: " . person.name
		. "`nStatus: " . person.status
		. "`nScore: " . person.score
}

Code: Select all

I used
for index, person in People {
    name := person.name
	StringReplace , name, name, %A_Space%,,All
    %name% := 1
}
But it's not wroking.
AutoTrim, on is not working either..
error.png
error.png (1.53 KiB) Viewed 2962 times
^^ What is wrong??
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Ughm help

12 Apr 2018, 09:01

Edit; nvm
Last edited by Nwb on 12 Apr 2018, 09:54, edited 1 time in total.
I am your average ahk newbie. Just.. a tat more cute. ;)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Ughm help

12 Apr 2018, 09:34

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)

splitArray := StrSplit(TestTexti, "`n")

People := {}

Loop, % splitArray.MaxIndex() {

	if (Mod(A_Index, 2) == 0) {
		continue
	}

	person := {}
	person.name := splitArray[A_Index]
	person.status := splitArray[A_Index + 1]

	RegExMatch(person.status, "\d+", score)
	person.score := score
	person.isRegistered := false

	People.push(person)
}

for index, person in People {
	MsgBox, % "index: " . index 
		. "`nName: " . person.name
		. "`nStatus: " . person.status
		. "`nScore: " . person.score
		. "`nisRegistered: " . (person.isRegistered ? "Already registered." : "Not yet registered.")
}

registerTheFirstPerson()

MsgBox, % "Is the 1st person registered now?`r`n" . (People[1].isRegistered ? "Yes, registered." : "No, not registered.")

MsgBox, % "What about the rest of them?"

for index, person in People {
	MsgBox, % "index: " . index 
		. "`nName: " . person.name
		. "`nStatus: " . person.status
		. "`nScore: " . person.score
		. "`nisRegistered: " . (person.isRegistered ? "Already registered." : "Not yet registered.")
}
Return

registerTheFirstPerson() {
	global People ; gain access to the collection from outside the function

	theFirstPerson := People[1]

	; dont do anything if already registered
	if (theFirstPerson.isRegistered) { ; alternatively, do (People[1].isRegistered) if you dont need the extra variable
		return
	}

	; do something with the unregistered person

	theFirstPerson.isRegistered := true ; change the persons flag, indicating theyve been registered
}
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Ughm help

12 Apr 2018, 09:55

swagfag wrote:

Code: Select all

TestTexti =
( 
Name1
Status - (10/10)
Name2
Status - (4/10)
Name3
Status - (6/10)
)

splitArray := StrSplit(TestTexti, "`n")

People := {}

Loop, % splitArray.MaxIndex() {

	if (Mod(A_Index, 2) == 0) {
		continue
	}

	person := {}
	person.name := splitArray[A_Index]
	person.status := splitArray[A_Index + 1]

	RegExMatch(person.status, "\d+", score)
	person.score := score
	person.isRegistered := false

	People.push(person)
}

for index, person in People {
	MsgBox, % "index: " . index 
		. "`nName: " . person.name
		. "`nStatus: " . person.status
		. "`nScore: " . person.score
		. "`nisRegistered: " . (person.isRegistered ? "Already registered." : "Not yet registered.")
}

registerTheFirstPerson()

MsgBox, % "Is the 1st person registered now?`r`n" . (People[1].isRegistered ? "Yes, registered." : "No, not registered.")

MsgBox, % "What about the rest of them?"

for index, person in People {
	MsgBox, % "index: " . index 
		. "`nName: " . person.name
		. "`nStatus: " . person.status
		. "`nScore: " . person.score
		. "`nisRegistered: " . (person.isRegistered ? "Already registered." : "Not yet registered.")
}
Return

registerTheFirstPerson() {
	global People ; gain access to the collection from outside the function

	theFirstPerson := People[1]

	; dont do anything if already registered
	if (theFirstPerson.isRegistered) { ; alternatively, do (People[1].isRegistered) if you dont need the extra variable
		return
	}

	; do something with the unregistered person

	theFirstPerson.isRegistered := true ; change the persons flag, indicating theyve been registered
}
Thanks a lot I will look at it later

btw i know this is a scripting forum so everybody is completely formal.. but why do you help people? It just amazes me especially autohotkey itself because it's a non-profit and the developers spend so much time and take no credit! It's just amazing. It's so inspiring :salute: and you are willing to help me even though I'm lazy and haven't read so much of autohotkey's documentation yet.

I have so many posts and majority are asking for help. I guess this vacation I will read through the documentation properly this time and start helping people in the gaming subforum (becuz it's usually the more simple requests) I find helping as a way to learn. I don't "practice'' but pick specific scripts and see how to do them I want to know how you people think whether you help for being kind or help for building a character etc.

Thanks for everything swag :thumbup:
I am your average ahk newbie. Just.. a tat more cute. ;)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 134 guests