Push First A_LoopField to array and secound to another array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Push First A_LoopField to array and secound to another array

Post by HiSoKa » 04 Dec 2022, 12:58

I have data,
How can I put the first element in the array First_Name, and second element in the array Last_Name,
And the third in the array First_Name, the fourth in the array Last_Name and so on...

Code: Select all

Data =
(
Name1|Lname1|Name2|Lname2|Name3|Lname3|Name4|Lname4|Name5|Lname5|Name6|Lname6|Name7|Lname7|Name8|Lname8|
)
First_Name := []
Last_Name  := []
Loop, Parse, Data, "|"
    First_Name.Push(A_LoopField)

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Push First A_LoopField to array and secound to another array  Topic is solved

Post by Smile_ » 04 Dec 2022, 13:16

Code: Select all

Data =
(
Name1|Lname1|Name2|Lname2|Name3|Lname3|Name4|Lname4|Name5|Lname5|Name6|Lname6|Name7|Lname7|Name8|Lname8|
)
First_Name := []
Last_Name  := []
Loop, Parse, Data, "|"
    Mod(A_Index, 2) = 1 ? First_Name.Push(A_LoopField) : Last_Name.Push(A_LoopField)

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Push First A_LoopField to array and secound to another array

Post by HiSoKa » 04 Dec 2022, 13:44

Thanks @Smile_ :salute:


User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Push First A_LoopField to array and secound to another array

Post by Chunjee » 04 Dec 2022, 14:34

HiSoKa wrote:
04 Dec 2022, 12:58
How can I put the first element in the array First_Name, and second element in the array Last_Name
I would recommend a table. This ensures related elements stay in sync

Image

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Push First A_LoopField to array and secound to another array

Post by HiSoKa » 04 Dec 2022, 14:35

Sorry @Smile_ for the inconvenience,
But how can I do the same if I have the phone numbers for the same persons,
That is, the process must be the first element for First_Name, the second for Last_Name, and the third for Phone_Number..
I tried to do this myself, but I can't figure out.

Code: Select all

Data =
(
Name1|Lname1|123456789|Name2|Lname2|123456789|Name3|Lname3|123456789|Name4|Lname4|123456789|
)

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Push First A_LoopField to array and secound to another array

Post by HiSoKa » 04 Dec 2022, 14:40

Chunjee wrote:
04 Dec 2022, 14:34
I would recommend a table. This ensures related elements stay in sync
I agree, but how i can create table, do you mean using
[Class] SQLiteDB from just me

by the way i like the photo :lol:

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Push First A_LoopField to array and secound to another array

Post by Smile_ » 04 Dec 2022, 15:09

You can do this:

Code: Select all

Data =
(
Name1|Lname1|123456789|Name2|Lname2|123456789|Name3|Lname3|1234567899|Name4|Lname4|123456789|
)
First_Name  := []
Last_Name   := []
Number      := []

Loop, Parse, Data, "|"
{
    Switch Mod(A_Index, 3) {
        Case 1 : First_Name.Push(A_LoopField)
        Case 2 : Last_Name.Push(A_LoopField)
        Case 0 : Number.Push(A_LoopField)
    }
}
But I recommend this:

Code: Select all

Data := { person1 : { name   : "Name1"
                    , lname  : "LName1"
                    , number : "123456789"} 
           
        , person2 : { name   : "Name2"
                    , lname  : "LName2"
                    , number : "123456789"} 
         
        , person3 : { name   : "Name3"
                    , lname  : "LName3"
                    , number : "123456789"} 
                  
        , person4 : { name   : "Name4"
                    , lname  : "LName4"
                    , number : "123456789"} }

For Person, Info in Data
    Msgbox, % Person ":"
            . "`nname: "    Info.name
            . "`nlname: "   Info.lname
            . "`nnumber: "  Info.number
So you can treat each person info separately.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Push First A_LoopField to array and secound to another array

Post by HiSoKa » 04 Dec 2022, 15:20

Thank you, I know the second method.
But I'm trying to figure out how I can deal with regular arrays..
I appreciate your help and time, Thanks you again...
problem solved ..

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Push First A_LoopField to array and secound to another array

Post by Smile_ » 04 Dec 2022, 15:25

In the first method I used the rest of the division of A_Index / the number of the info, always 0, 1, or 2 for your last example, and then push the values into the arrays depend on it.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Push First A_LoopField to array and secound to another array

Post by HiSoKa » 04 Dec 2022, 15:29

Smile_ wrote:
04 Dec 2022, 15:25
In the first method I used the rest of the division of A_Index / the number of the info, always 0, 1, or 2 for your last example, and then push the values into the arrays depend on it.
I think Mod is very necessary because I have seen it in more than one example, And I do not know how to use it and I don't know what it is for :mrgreen: ,
I will read about it in the documents. Thanks

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Push First A_LoopField to array and secound to another array

Post by Chunjee » 04 Dec 2022, 15:43

I noticed some of the code ITT leaves a trailing empty element: , ""] because the last | in the data has nothing after it

HiSoKa wrote:
04 Dec 2022, 14:40
I agree, but how i can create table
How I might parse it into a table:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

myDataStruct := []
Data =
(
Name1|Lname1|123456789|Name2|Lname2|123456789|Name3|Lname3|123456789|Name4|Lname4|123456789|
)

chunks := A.chunk(strSplit(Data, "|"), 3)
for key, value in chunks {
	if (value.1 != "") {
		myDataStruct.push({"firstName":value.1, "lastName":value.2, "phone":value.3})
	}
}
; => [{"firstName":"Name1", "lastName":"Lname1", "phone":"123456789"}
;	, {"firstName":"Name2", "lastName":"Lname2", "phone":"123456789"}
;	, {"firstName":"Name3", "lastName":"Lname3", "phone":"123456789"}
;	, {"firstName":"Name4", "lastName":"Lname4", "phone":"123456789"}]

You can also call a table an "Array of Objects"
That's how I like to think of it.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Push First A_LoopField to array and secound to another array

Post by HiSoKa » 04 Dec 2022, 15:55

Nice code, i will try it..
Thanks you

Post Reply

Return to “Ask for Help (v1)”