How to get values from these arrays ?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

How to get values from these arrays ?

25 Oct 2023, 09:47

Hi,

I'm really struggling with below code, perhaps someone can help me out ?
What is the correct syntax to show the country on the first line of the examples text (Utopia) ??

If you want to test the code, please copy below text to clipboard and then press CTRL-1, after it finishes, press CTRL-2

I'm struggling with this part:

Code: Select all

    MsgBox(SplitLines.No[8])    ; this shows Btopia< the last line of the text how do I get the value of the line above it ?
    MsgBox(SplitLines2[8])     ; I thought this should work, but it doesn't. What is the mistake ??


full code

Code: Select all

;========================================================================
#Requires AutoHotkey v2.0
;========================================================================
;;;;;;;;;;;;;;;;;;PLEASE CODE BELOW TEXT AND THEN RUN THE CODE AND PRESS CTRL-1, AFTER IT'S FINISHED PRESS CTRL-2;;;;;;;;;;;;;;;;;;
/*
Interne ID document	Klant-/leveranciersnaam	Provincie	Gebouw	Straat	Postcode	Plaats	Land	Artikelnummer	Naam in vreemde taal	Hoeveelheid	Regeltotaal EUR	Regeltotaal (VV)	Prijsvaluta	Gewicht 1	Documentnummer	HS-Number	Land van herkomst	
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	Utopia	PS6	PlayStation 6	1,00	68,16	72,00	USD	1,4700	5772440	4820105000	China	
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	BUtopia	P	Packing and delivery	1,00	27,45	29,00	USD	0,1000	5772440			
*/  
;;;;;;;;;;;;;;;;;;PLEASE CODE BELOW TEXT AND THEN RUN THE CODE AND PRESS CTRL-1, AFTER IT'S FINISHED PRESS CTRL-2;;;;;;;;;;;;;;;;;;

DHL_Data_Lines  := []
SplitLines      := []
No  := 1

^1::

{
    global
    if !InStr(A_Clipboard, "Interne ID document")               ; Clipboard must contain the text "Interne ID document, otherwise reload the script"
        Reload
    else
        {
            DHL_Data_Lines := StrSplit(A_Clipboard, "`n")       ; store each line in the an array
            Loop DHL_Data_Lines.Length
                {
                    
                    if (DHL_Data_Lines[A_Index]) != ""
                        {
                            SplitLines.No := StrSplit(DHL_Data_Lines[A_Index], A_Tab)
                        }
                    else
                        {
                            DHL_Data_Lines.RemoveAt(A_Index)    ; remove line from array if it's empty (not sure if working)
                        }    
                    Sleep 2000    
                    Test := SplitLines.No[8]
                    ToolTip("Test :=" . Test)
                    No++
                }
        }
    ;SplitLines.No := StrSplit(DHL_Data_Lines[1], A_Tab)
    
    
    Goto MyLabel

    MyLabel:
    ;MsgBox("No := " . No "-" . SplitLines2[4])    
    ;MsgBox(SplitLines.2[3])
    return
}

^2::
{
    if !InStr(A_Clipboard, "Interne ID document")               ; Clipboard must contain the text "Interne ID document, otherwise reload the script"
        Reload
    MsgBox(SplitLines.No[8])    ; this shows Btopia< the last line of the text how do I get the value of the line above it ?
    MsgBox(SplitLines2[8])     ; I thought this should work, but it doesn't. What is the mistake ??
    
}

ESC::Reload
User avatar
WarlordAkamu67
Posts: 231
Joined: 21 Mar 2023, 06:52

Re: How to get values from these arrays ?

25 Oct 2023, 11:03

I trimmed out some of what was not relevant to the setup. I turned "Test" into an array and pushed the value of "SplitLines[8]" into it for each loop.

Code: Select all

;========================================================================
#Requires AutoHotkey v2.0
;========================================================================
;;;;;;;;;;;;;;;;;;PLEASE CODE BELOW TEXT AND THEN RUN THE CODE AND PRESS CTRL-1, AFTER IT'S FINISHED PRESS CTRL-2;;;;;;;;;;;;;;;;;;
/*
Interne ID document	Klant-/leveranciersnaam	Provincie	Gebouw	Straat	Postcode	Plaats	Land	Artikelnummer	Naam in vreemde taal	Hoeveelheid	Regeltotaal EUR	Regeltotaal (VV)	Prijsvaluta	Gewicht 1	Documentnummer	HS-Number	Land van herkomst
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	Utopia	PS6	PlayStation 6	1,00	68,16	72,00	USD	1,4700	5772440	4820105000	China
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	BUtopia	P	Packing and delivery	1,00	27,45	29,00	USD	0,1000	5772440
*/
;;;;;;;;;;;;;;;;;;PLEASE CODE BELOW TEXT AND THEN RUN THE CODE AND PRESS CTRL-1, AFTER IT'S FINISHED PRESS CTRL-2;;;;;;;;;;;;;;;;;;

DHL_Data_Lines  := []
Test      := []

^1:: {
    global
    if !InStr(A_Clipboard, "Interne ID document") {               ; Clipboard must contain the text "Interne ID document, otherwise reload the script"
      Reload
} else {
      DHL_Data_Lines := StrSplit(A_Clipboard, "`n")       ; store each line in the an array
      Loop DHL_Data_Lines.Length {
        if ((DHL_Data_Lines[A_Index]) != "") {
          SplitLines := StrSplit(DHL_Data_Lines[A_Index], A_Tab)
} else {
          DHL_Data_Lines.RemoveAt(A_Index)    ; remove line from array if it's empty (not sure if working)
}
        Sleep(2000)
        Test.Push(SplitLines[8])
        ToolTip("Test :=" . Test[A_Index])
}
}
    return
}

^2:: {
  global
  if !InStr(A_Clipboard, "Interne ID document") {             ; Clipboard must contain the text "Interne ID document, otherwise reload the script"
    Reload
}
  For index, text in Test {
    MsgBox(Test[index])
}
  Return
}

ESC::Reload
User avatar
WarlordAkamu67
Posts: 231
Joined: 21 Mar 2023, 06:52

Re: How to get values from these arrays ?

25 Oct 2023, 11:21

This will create an array called "SplitLines" which will be an array of arrays. SplitLines[1] refers to the first Array. SplitLines[1][2] refers to the 2nd element in the array of SplitLines[1]. In this case the name "Klant-/lev..."

Code: Select all

;========================================================================
#Requires AutoHotkey v2.0
;========================================================================
;;;;;;;;;;;;;;;;;;PLEASE CODE BELOW TEXT AND THEN RUN THE CODE AND PRESS CTRL-1, AFTER IT'S FINISHED PRESS CTRL-2;;;;;;;;;;;;;;;;;;
/*
Interne ID document	Klant-/leveranciersnaam	Provincie	Gebouw	Straat	Postcode	Plaats	Land	Artikelnummer	Naam in vreemde taal	Hoeveelheid	Regeltotaal EUR	Regeltotaal (VV)	Prijsvaluta	Gewicht 1	Documentnummer	HS-Number	Land van herkomst
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	Utopia	PS6	PlayStation 6	1,00	68,16	72,00	USD	1,4700	5772440	4820105000	China
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	BUtopia	P	Packing and delivery	1,00	27,45	29,00	USD	0,1000	5772440
*/
;;;;;;;;;;;;;;;;;;PLEASE CODE BELOW TEXT AND THEN RUN THE CODE AND PRESS CTRL-1, AFTER IT'S FINISHED PRESS CTRL-2;;;;;;;;;;;;;;;;;;

DHL_Data_Lines  := []
SplitLines := []

^1:: {
    global
    if !InStr(A_Clipboard, "Interne ID document") {               ; Clipboard must contain the text "Interne ID document, otherwise reload the script"
      Reload
} else {
      DHL_Data_Lines := StrSplit(A_Clipboard, "`n")       ; store each line in the an array
      Loop DHL_Data_Lines.Length {
        if ((DHL_Data_Lines[A_Index]) != "") {
          SplitLines.Push(StrSplit(DHL_Data_Lines[A_Index], A_Tab))
} else {
          DHL_Data_Lines.RemoveAt(A_Index)    ; remove line from array if it's empty (not sure if working)
}
        Sleep(2000)
        ToolTip("Test :=" . SplitLines[A_Index][8])
}
}
    return
}

^2:: {
  global
  if !InStr(A_Clipboard, "Interne ID document") {             ; Clipboard must contain the text "Interne ID document, otherwise reload the script"
    Reload
}
  For index, text in SplitLines {
    MsgBox(SplitLines[index][8])
}
  MsgBox(SplitLines[1][1] " is the id number of the first line.")
  MsgBox(SplitLines[2][1] " is the id number for the second line. Note this is Index 1 of our 2nd SplitLines")
  MsgBox(SplitLines[3][1] " is the id number again. SplitLine[3] is the line. [1] is the index of the information. The ID number in this case")
  MsgBox(SplitLines[1][2] " your name?")
  MsgBox(SplitLines[2][2] " your name?")
  MsgBox(SplitLines[3][2] " your name?")
  Return
}

ESC::Reload
vmech
Posts: 371
Joined: 25 Aug 2019, 13:03

Re: How to get values from these arrays ?

25 Oct 2023, 11:24

Minimal code changes. Just simple syntax corrections (and some questions inside code commentaries :lol: )

Code: Select all

;========================================================================
;;;;;;;;;;;;;;;;;;PLEASE CODE BELOW TEXT AND THEN RUN THE CODE AND PRESS CTRL-1, AFTER IT'S FINISHED PRESS CTRL-2;;;;;;;;;;;;;;;;;;
/*
Interne ID document	Klant-/leveranciersnaam	Provincie	Gebouw	Straat	Postcode	Plaats	Land	Artikelnummer	Naam in vreemde taal	Hoeveelheid	Regeltotaal EUR	Regeltotaal (VV)	Prijsvaluta	Gewicht 1	Documentnummer	HS-Number	Land van herkomst	
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	Utopia	PS6	PlayStation 6	1,00	68,16	72,00	USD	1,4700	5772440	4820105000	China	
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	BUtopia	P	Packing and delivery	1,00	27,45	29,00	USD	0,1000	5772440			
*/  
;;;;;;;;;;;;;;;;;;PLEASE CODE BELOW TEXT AND THEN RUN THE CODE AND PRESS CTRL-1, AFTER IT'S FINISHED PRESS CTRL-2;;;;;;;;;;;;;;;;;;

#Requires AutoHotkey v2.0
DHL_Data_Lines  := []
SplitLines      := []
No  := 1

^1::
{
  global
  if !InStr(A_Clipboard, "Interne ID document")	; Clipboard must contain the text "Interne ID document", otherwise reload the script
    Reload
  else
  {
    DHL_Data_Lines := StrSplit(A_Clipboard, "`n")	; store each line in the an array
    Loop DHL_Data_Lines.Length
    {
      if (DHL_Data_Lines[A_Index] != "")
      {
        SplitLines.Push(StrSplit(DHL_Data_Lines[A_Index], A_Tab))
      }
      ;;; *** Else branch is really necessary ???!!! ***
      ;;; Read <Loop Count evaluation> in https://www.autohotkey.com/docs/v2/lib/Loop.htm#Parameters
      else
      {
        DHL_Data_Lines.RemoveAt(A_Index)	; remove line from array if it's empty (not sure if working)
      }
      Test := SplitLines[No][8]
      ToolTip("Test :=" . Test)
      Sleep 2000
      ToolTip
      No++
    }
  }
  ; SplitLines.No := StrSplit(DHL_Data_Lines[1], A_Tab)

  ; Goto MyLabel

  ; MyLabel:
  MsgBox("No := " No " - " SplitLines[2][4])    
  MsgBox(SplitLines[2][3])
  ; return
}

^2::
{
  if !InStr(A_Clipboard, "Interne ID document")	; Clipboard must contain the text "Interne ID document, otherwise reload the script"
    Reload

  ; *** WHAT is this <SplitLines.Data> ???
  ; MsgBox(SplitLines[No][1] "." A_Tab SplitLines.Data[1])	; this shows Btopia< the last line of the text how do I get the value of the line above it ?

  MsgBox(SplitLines[2][8])	; I thought this should work, but it doesn't. What is the mistake ??
}

Esc::Reload
PS. I deliberately did not fix 2 non-critical errors in this code.
Last edited by vmech on 25 Oct 2023, 12:19, edited 1 time in total.
Please post your script code inside [code] ... [/code] block. Thank you.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to get values from these arrays ?

25 Oct 2023, 12:17

Thanks guys. I will review your code tomorrow since I don't have access to my pc right now.
User avatar
flyingDman
Posts: 2844
Joined: 29 Sep 2013, 19:01

Re: How to get values from these arrays ?

25 Oct 2023, 14:24

Your clipboard may contain one or more empty line and some lines have 18 fields, while others have 17. This is an issue. When row 2 is blank for instance, calling arr[2][8] results in an Invalid Index Error (and does it before you can delete the line; and b.t.w, deleting might not be the right solution anyway)
Or when you call arr[4][18] it will also throw an error because there is no "Land van herkomst".
The following avoids these errors (credit @teadrinker ):

Code: Select all

#Requires AutoHotkey v2.0
var := "             ;using var instead of clipboard for testing purposes
(
Interne ID document	Klant-/leveranciersnaam	Provincie	Gebouw	Straat	Postcode	Plaats	Land	Artikelnummer	Naam in vreemde taal	Hoeveelheid	Regeltotaal EUR	Regeltotaal (VV)	Prijsvaluta	Gewicht 1	Documentnummer	HS-Number	Land van herkomst

953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	Utopia	PS6	PlayStation 6	1,00	68,16	72,00	USD	1,4700	5772440	4820105000	China
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	BUtopia	P	Packing and delivery	1,00	27,45	29,00	USD	0,1000	5772440
953012	Kevin Baker			20 Riverbend Rd.	12345	FairyTale City	BUtopia	P	Packing and delivery	1,00	27,45	29,00	USD	0,1000	5772440
)"

Array.Prototype.DefineProp('Default', {Value: ""})				; could be Value: "n/a" or "n.v.t" in Dutch
arr := []
for x,y in strsplit(var,"`n","`r")
	{
	z := strsplit(y,a_tab)
	z.Length := 18						                        ; "sets" the array length of each line to 18
	arr.push(z)
	}
msgbox arr[2][8]			; empty string
msgbox arr[4][18]			; empty string
msgbox arr[3][8]			; Utopia
Although @lexikos warns about the usage of this method here: viewtopic.php?f=82&t=122222&p=542802&hilit=Array.Prototype.DefineProp#p543256, I would not know how to correctly remedy the issue.
14.3 & 1.3.7
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: How to get values from these arrays ?

26 Oct 2023, 09:09

Great stuff guys ! I think I'm going to use @flyingDman's example to build my script.

Let me explain what I'm trying to achieve.
For work I have to manually fill out forms for customs purposes on the DHL website.

In our ERP system (SAP Business One), I have created an SQL query that lists all the information I need. It resembles the data I posted at the top on my script, but it could also have more lines (or less).
I would like my script to be able to push each line into this form by way of some key combination. The price articlenumber 'P' (Shipping cost) should end up in the 'Postage Cost' field and not
on the line where the articles are.

I'll report back when I need more help, or perhaps I'll open a new topic (might be better).
ahk2.png
ahk2.png (46.84 KiB) Viewed 305 times

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 34 guests