how to assign a value to a number based on it's value

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
thenriques

how to assign a value to a number based on it's value

29 Jul 2017, 10:29

This is supposed to be a small issue but 've been at this for hours -_-

This is a sinept of a bigger code. that code has a problem and I was trying to solve the problem without having to rewrite the whole code.

In order to solve the problem I need to teel the code to associate a position with each number.

Example:
For this sequence:
sim := "1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9
"

I need the code to associate:

Code: Select all

1 - 1
10 - 10
11 - 11
12 - 12
13 - 13
14 - 14
15 - 15
16 - 16
17 - 17
18 - 18
19 - 19
2 - 2
20 - 20
21 - 21
22 - 22
3 - 3
4 - 4
5 - 5
6 - 6
7 - 7
8 - 8
9 - 9
1 - 23
10 - 32
11 - 33
12 - 34
13 - 35
14 - 36
15 - 37
16 - 38
17 - 39
18 - 40
19 - 41
2 - 24
20 - 42
21 - 43
22 - 44
3 - 25
4 - 26
5 - 27
6 - 28
7 - 29
8 - 30
9 - 31
1 - 45
10 - 54
11 - 55
12 - 56
13 - 57
14 - 58
15 - 59
16 - 60
17 - 61
18 - 62
19 - 63
2 - 46
20 - 64
21 - 65
22 - 66
3 - 47
4 - 48
5 - 49
6 - 50
7 - 51
8 - 52
9 - 53
1 - 67
10 - 76
11 - 77
12 - 78
13 - 79
14 - 80
15 - 81
16 - 82
17 - 83
18 - 84
19 - 85
2 - 68
20 - 86
21 - 87
22 - 88
3 - 69
4 - 70
5 - 71
6 - 72
7 - 73
8 - 74
9 - 75
The logic is:

If a number is less than the highest number so far:
associate it with a postion matching its own value:

Code: Select all

1 - 1
10 - 10
11 - 11
12 - 12
13 - 13
14 - 14
15 - 15
16 - 16
17 - 17
18 - 18
19 - 19
2 - 2
20 - 20
21 - 21
22 - 22
3 - 3
4 - 4
5 - 5
6 - 6
7 - 7
8 - 8
9 - 9
If the next number is equal to one, this means that we have entered a new sequence. Therefore we need to associate to it a position which has to be equal to that number plus the highest number so far:

Code: Select all

1 - 23
10 - 32
11 - 33
12 - 34
13 - 35
14 - 36
15 - 37
16 - 38
17 - 39
18 - 40
19 - 41
2 - 24
20 - 42
21 - 43
22 - 44
3 - 25
4 - 26
5 - 27
6 - 28
7 - 29
8 - 30
9 - 31
1 - 45
10 - 54
11 - 55
12 - 56
13 - 57
14 - 58
15 - 59
16 - 60
17 - 61
18 - 62
19 - 63
2 - 46
20 - 64
21 - 65
22 - 66
3 - 47
4 - 48
5 - 49
6 - 50
7 - 51
8 - 52
9 - 53

When another one is found, we repeat the procedure, adding the highest number before the one appearing to the subsequent numbers (until a new one is found)

Code: Select all

1 - 67
10 - 76
11 - 77
12 - 78
13 - 79
14 - 80
15 - 81
16 - 82
17 - 83
18 - 84
19 - 85
2 - 68
20 - 86
21 - 87
22 - 88
3 - 69
4 - 70
5 - 71
6 - 72
7 - 73
8 - 74
9 - 75
My code so far:

Code: Select all

#SingleInstance, Force
SetTitleMatchMode, 2
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.



sim := "1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9"
StringSplit, sim, sim, %a_space%



t := 0
x := 0
listing := 1
messagebox := ""
loop %sim0%
{
	x := x + 1
	big%x% := 0
}
loop %sim0%
{
	t := t + 1
	value := sim%t%
	position := sim%t% ; simulates	h := RegExReplace(ACTIVIDADE_%z%_TITULO__FICHEIRO%y%, "\D") ; catch the number in the filename
	
	
	


		if (position = 1)
		{
			listing := listing + 1
		}
		
		previouslisting := listing - 1
		if (listing = 2)
		{
			position := position + big%previouslisting%
			;~ listing := 3
		}
		
		big := big%listing%
		if (position >= big){
            big := position 
			 ;~ msgBox,,, % big
         }

		messagebox .= "value: " . value . " --- position: " . position . "`r`n"
		 
	}
	MsgBox,,, %messagebox%
exitapp	 
		 
^Esc::
ExitApp
return
It doesn't work :|
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: how to assign a value to a number based on it's value

29 Jul 2017, 12:30

Hi, try this :)

Code: Select all

#NoEnv
#Warn
#SingleInstance Force

sim := "1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9"

; Call the 'MakeListing' function. It returns a 'Listing' object.
Sim := MakeListing(sim)

; For each item in the 'Listing' object...
Output := ""
for i, v in Sim
    Output .= "<" . i . ">" . A_Tab . v.Number . A_Tab . "-" . A_Tab . v.Value . "`r`n"

; Display the output in Notepad.
Run, Notepad,,, NotepadPID
WinWait, % "ahk_pid " . NotepadPID
ControlSetText, Edit1, % Output, % "ahk_pid " . NotepadPID
ExitApp


MakeListing(s) {
    ; Initial values.
    Listing := []  ; Create an object. (https://autohotkey.com/docs/Objects.htm#Usage)
    LargestValue := 0
    NewLargestValue := 0

    ; For each number in the input sting...
    for Position, _Number in StrSplit(s, A_Space) {
        
        ; If this number is 1 then update 'LargestValue'.
        if (_Number = 1)
            LargestValue := NewLargestValue
        
        ;Calculate the current value.
        _Value := _Number + LargestValue
        
        ; Push an object with keys 'Number' and 'Value' into 'Listing'
        Listing.Push( {Number: _Number, Value: _Value} )
        
        ; If the current value is greater than 'NewLargestValue' then update it. 
        if (_Value > NewLargestValue)
            NewLargestValue := _Value
    }
    ; Return the 'Listing' that was created. 
    return Listing
}
Edit: Added comments.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to assign a value to a number based on it's value

26 Aug 2017, 12:19

I'm not entirely sure what was wanted, this script gives the same results as the script above. If I had more idea of what was needed, then perhaps I could provide something better.

Code: Select all

q::
vList := "1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9"
oArray := {}, vOutput := "", vIndex := 1
Loop, 4
{
	Loop, Parse, vList, % " "
	{
		if oArray.HasKey(A_LoopField)
			oArray[A_LoopField] += 22
		else
			oArray[A_LoopField] := A_LoopField
		vOutput .= vIndex "`t" A_LoopField "`t" oArray[A_LoopField] "`r`n"
		vIndex++
	}
}
Clipboard := vOutput
MsgBox, % "done"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR, demon740, gsxr1300, ht55cd3 and 274 guests