merging 2 arrays

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

merging 2 arrays

17 Nov 2017, 07:00

I want that the code will check if there is the same date from array1 in array2 and if so, then the code will transfer value near date from array2 to array1 date and value. And if the date in some array is unique and doesn't repeats in the other array, it should be included in the final form too. Like:

Code: Select all

cArray := {}
array1 := "15.11.2017	3, 14.11.2017	3, 03.10.2017	2, 03.11.2017	1, 14.10.2017	2, 20.10.2017	1, 08.11.2017	1, 21.10.2017	1,"
array2 := "14.11.2017		1, 15.11.2017	2, 20.10.2017	9, 21.10.2017	1,"
xArray := StrSplit(array1 , ",")
yArray := StrSplit(array2 , ",")
The final result should be something like:
14.11.2017 1; 1
15.11.2017 2; 4
20.10.2017 9; 1
21.10.2017 1;4
03.10.2017 1;0
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: merging 2 arrays

17 Nov 2017, 08:54

what I have right now. I can gather information in 2 arrays about the database. But I don't know how to merge those arrays to get a result of:
16.10.2017: 1 , 0
20.10.2017: 3 , 4
15.11.2017: 0 ,1

Code: Select all

blok =
(
15 11 2017 Other
16 10 2017 Fel   
20 10 2017 Other     
20 10 2017 Other      
20 10 2017 Other
20 10 2017 Other      
20 10 2017 Fel
20 10 2017 Fel
20 10 2017 Fel
)
oBlock := [] 
oBlock2 := []
		loop, parse, blok, `n, `r
		{
			line := A_LoopField
			StringSplit, linee, line, %A_Space%
			ndata = %linee1%.%linee2%.%linee3%
			If InStr(line, "Fel")
					oBlock2[ndata] := oBlock2[ndata] ? oBlock2[ndata] + 1 : 1
			If !InStr(line, "Fel")
					oBlock[ndata] := oBlock[ndata] ? oBlock[ndata] + 1 : 1
		}
		for day, frequency in oBlock2
			res .= day ": " frequency "`n"
		for day, frequencyy in oBlock
			res2 .= day ": " frequencyy "`n"
MsgBox %res%`n`n%res2%
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: merging 2 arrays

17 Nov 2017, 20:02

I cannot understand what you want enough to provide an answer.

I don't understand what the number after the ; means in your final result of first post.

Your second post has different format for your input data and a different output format of the results.

You need to provide exact code examples of your two sources of data and exactly what the output should be.

Unravelling what you actually want is a greater challenge than the actually coding of a solution.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: merging 2 arrays

17 Nov 2017, 21:48

You could add one array to the other.. But you could also simply use both arrays normally for the output.

Merging arrays:

Code: Select all

#singleInstance force
#persistent
#include *i <Lib_1>

array1:=[1,2,3,4,5]
array2:=[2,4,6]

loop % array2.length() ; iterate through the array2
    array1.push(array2.pop()) ; remove the last entry of array2 and put it at the end of array1

; display array1
out:=
for i,a in array1
    out.=a
msgbox % out
exitApp
This is one of the most efficient methods, due to pop() both returning and removing the last element of the array. However, this does mean it's merging it in backwards.

As far as sorting for the final output goes, writing a method to iterate through and count duplicates seems to be what you want. Or you could parse the array into a CSV format and use Sort.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: merging 2 arrays

18 Nov 2017, 01:34

Masonjar13 wrote:Merging arrays:

Code: Select all

#singleInstance force
#persistent
#include *i <Lib_1>

array1:=[1,2,3,4,5]
array2:=[2,4,6]

loop % array2.length() ; iterate through the array2
    array1.push(array2.pop()) ; remove the last entry of array2 and put it at the end of array1

; display array1
out:=
for i,a in array1
    out.=a
msgbox % out
exitApp
This is one of the most efficient methods, due to pop() both returning and removing the last element of the array. However, this does mean it's merging it in backwards.
To have the second array not be backwards, you could use RemoveAt(1) instead of Pop().

I view this as more of a concatenate than a merge.

This is what I would call a merge of associative arrays:

Code: Select all

Array1 := {one: 123, two: 456, three: 789}
Array2 := {three: 987, four: 654, five: 321}

for key, val in Array2
	Array1[key] := val

for key, val in Array1
	Display .= key "`t" val "`n"

MsgBox % Display
I imagine a merge of a simple array would be more a pushing of values that are not in the values of the first array.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: merging 2 arrays

18 Nov 2017, 06:42

I think this is OT, but for linear arrays, to merge / append two arrays, a.push(b*).

Cheers.
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: merging 2 arrays

18 Nov 2017, 11:37

I will try to explain it better. The code copy a block from a program and checks each line. The line begans with a date and the following information. If the following information has some points, then the code should COUNT this line as +1 to that date which is in the first place in the line. So, the code takes a block, loop through the block, counts the lines which should be added. But I met a problem when I want to separate how many types came each day. In Array I can add a key and a value, but here I need to have 2 values in one array. The logic should be if param = firstoption ? firstvalue+1 and secondvalue+0 or if param = secondoption ? firstvalue+0 and secondvalue+1. At the end I should have results like date = firstvalue and secondvalue
In my code bellow I can separatly take a count of correct lines for each param (firstvalue and secondvalue). But then I need to merge those two into one date = firstvalue and secondvalue. And I don't know how..

Code: Select all

blok =
(
15 11 2017 Other
16 10 2017 Fel   
20 10 2017 Other     
20 10 2017 Other      
20 10 2017 Other
20 10 2017 Other      
20 10 2017 Fel
20 10 2017 Fel
20 10 2017 Fel
)
oBlock := [] 
oBlock2 := []
		loop, parse, blok, `n, `r
		{
			line := A_LoopField
			StringSplit, linee, line, %A_Space%
			ndata = %linee1%.%linee2%.%linee3%
			If InStr(line, "Fel")
					oBlock2[ndata] := oBlock2[ndata] ? oBlock2[ndata] + 1 : 1
			If !InStr(line, "Fel")
					oBlock[ndata] := oBlock[ndata] ? oBlock[ndata] + 1 : 1
		}
		for day, frequency in oBlock2
			res .= day ": " frequency "`n"
		for day, frequencyy in oBlock
			res2 .= day ": " frequencyy "`n"
MsgBox %res%`n`n%res2%

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, mikeyww, RussF and 310 guests