Accessing a very simple object by property variable Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Accessing a very simple object by property variable

25 Aug 2023, 11:36

This is the present code. I succeed in separating the single date in its three parts, but now want to look up cMonth (the month name) in my obj (newNumMonth)

I tried:

Code: Select all

newNumMonth.cMonth  // no good
newNumMonth.&cMonth // no good
newNumMonth[cMonth] // no good
newNumMonth[&cMonth] // no good
Edit: Spelling (2023-08-25 183825) ItisI
All gave me an error message "No such property"

Here's the code so far, a lot of msgboxes to see what's going on.

Code: Select all

#Requires AutoHotkey v2.0
#Include "udf.ahk"
#Include "UDF-test.ahk"

newNumMonth := {
    January: "01",
    February: "02",
    March: "03",
    April: "04",
    May: "05",
    June: "06",
    July: "07",
    August: "08",
    September: "09",
    October: "10",
    November: "11",
    December: "12"
}
cDate := ''
cDate := "15. April 1986"
cDay := ""
cDay := RegExReplace(cDate, "(^\d{1,2})\. [A-Za-z].+ \d{4}$", "$1")
cMonth := ''
cMonth := RegExReplace(cDate, "m)^\d{1,2}\. ([A-Za-z].+) \d{4}$", "$1")
cYear := RegExReplace(cDate, "^\d{1,2}\. [A-Za-z].+ (\d{4}$)", "$1")
MsgBox cYear . cMonth . cDay

MsgBox cMonth
cMonthValue := newNumMonth[cMonth]

MsgBox cMonthValue
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
mikeyww
Posts: 27194
Joined: 09 Sep 2014, 18:38

Re: Accessing a very simple object by property variable  Topic is solved

25 Aug 2023, 11:42

Code: Select all

cMonthValue := newNumMonth.%cMonth%

Code: Select all

#Requires AutoHotkey v2.0
newNumMonth := ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
cDate       := '15. April 1986'
part        := StrSplit(cDate, ' ')
moName      := part[2]
cMonthValue := moNum(moName)
MsgBox cMonthValue, moName, 64

moNum(moName) {
 For n, mo in newNumMonth
  If mo = SubStr(moName, 1, 3)
   Return n
}
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Accessing a very simple object by property variable

25 Aug 2023, 12:05

Thank you very much, this is the approach you followed in your original script. But it won't work for me once I incorporate month names like
Mar|Mär
May|Mai
Oct|Okt
Dec|Dez

I will have to add four extra key/value pairs and thus more elements pointing to "03", "05", "10", "12"
Edit 2023-08-25 190812

This function is supposed to convert 1 single selected date into "YYYY-MM-DD" - thanks to your help I can finish it now!

Thanks a lot :clap:
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
mikeyww
Posts: 27194
Joined: 09 Sep 2014, 18:38

Re: Accessing a very simple object by property variable

25 Aug 2023, 12:31

Code: Select all

#Requires AutoHotkey v2.0
newNumMonth := ['Jan', 'Feb', 'Mar|Mär', 'Apr', 'May|Mai', 'Jun'
              , 'Jul', 'Aug', 'Sep', 'Oct|Okt', 'Nov', 'Dec|Dez']
cDate := '15. April 1986'
cDate := '15. May 1986'
cDate := '15. Mai 1986'
part  := StrSplit(cDate, ' ')
date  := Format('{}-{:02}-{:02.0f}', part[3], moNum(part[2]), part[1])
MsgBox date, cDate, 64

moNum(moName) {
 For n, mo in newNumMonth
  If SubStr(moName, 1, 3) ~= 'i)' mo
   Return n
}
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Accessing a very simple object by property variable

26 Aug 2023, 01:05

@mikeyww
Thanks, looking at your approach yesterday I started incorporating your ideas, much more elegant. I have expanded my "newNumMonth" Obj by the pertinent key pairs, so in my object both "Mar" and "Mär" have the "month number" of "03". Also your way of using StrSplit is much more elegant and easier to read than my convoluted RegEx.

Will hopefully finish the function today and present the code.

Thank you :D
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
mikeyww
Posts: 27194
Joined: 09 Sep 2014, 18:38

Re: Accessing a very simple object by property variable

26 Aug 2023, 01:29

It seems like it is already done here, but OK if you have another way or something more.

Code: Select all

#Requires AutoHotkey v2.0
cDate := '15. Mai 1986'
MsgBox date(cDate), cDate, 64

date(str) {
 part := StrSplit(str, ' ')
 Return Format('{}-{:02}-{:02.0f}', part[3], moNum(part[2]), part[1])
}

moNum(moName) {
 Static newNumMonth := ['Jan', 'Feb', 'Mar|Mär', 'Apr', 'May|Mai', 'Jun'
                      , 'Jul', 'Aug', 'Sep', 'Oct|Okt', 'Nov', 'Dec|Dez']
 For n, mo in newNumMonth
  If SubStr(moName, 1, 3) ~= 'i)' mo
   Return n
}
image230826-0230-001_cr.png
Output
image230826-0230-001_cr.png (10.67 KiB) Viewed 413 times
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Accessing a very simple object by property variable

26 Aug 2023, 03:50

@mikeyww You've just bestowed upon me a lengthy reading list - I'm still catching up, will be back later ...

Edit - Added "Format" (2023-08-26 111327)
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Accessing a very simple object by property variable

26 Aug 2023, 08:58

The submitted form was invalid. Try submitting again.
Following (below!) my function it its present state. It is working fine, but I didn't go completely for your solution - here's why:

Code: Select all

date := Format('{}-{:02}-{:02.0f}', part[3], moNum(part[2]), part[1])
For the life of me I cannot figure out what this line does, nor how to get any variant of it to work for me. Of course I've tried to decipher Format, but what you are formatting, and how you are formatting it?

Your function moNum I somewhat understand

Code: Select all

moNum(moName) {
    For n, mo in newNumMonth {
        MsgBox n
        If SubStr(moName, 1, 3) ~= 'i)' mo //WHAT IS HAPPENING HERE?
            Return n
    }
but at "//" I don't understand the syntax. Also it seems, your function will go through your array 'newNumMonth' several times, in case of 'Dec" 12 times?

Is my approach not more efficient with a single lookup?

Code: Select all

#Requires AutoHotkey v2.0
#Include "F:\data\ahk\library\udf.ahk"
;#Include "F:\data\ahk\library\UDF-test.ahk"
/*--------------------------------------------------------------
PURPOSE:
Convert a one single _selected date_ (German or English)
to a format like "YYYY-MM-DD" (ISO 8601) like so

German:
"22. Dezember 1988" -> "1988-12-22"
"22. Dez. 1988" -> "1988-12-22"
English:
"December 22, 1988" -> "1988-12-22"
"Dec 22, 1988" -> "1988-12-22"

VARIABLES
The following varibles are in use:

- monthNum
  an object with 'month short' (Abbreviations for month names) and
  their corresponding 'month numbers' like so
  "Jun" -> "06"
  "Mar" -> "03"
  "Mär" -> "03"

- part
  Array. Populated by StrSplit in 3 parts
  part[1]: will hold the day of the date
  part[2]: will hold the month of the date
  part[3]: will hold the year of the date

- selectedDate
  This is the selected date string which is parsed into
  three parts (s.a.)
- use for readablity
    currentDay -> part[1]
    currentMonth -> part[2]
    currentYear -> part[3]
- datePattern
  Presently takes the value of "Deutsch" or "English"
  Used to distinguish repsp. date patterns
- currentMonthNum
  Takes the resp. number of the month ('May'->'05', 'Jun'->'06', asf)
--------------------------------------------------------------*/

F4:: {
    /*--------------------------------------------------------------
    Declaration of variables
    --------------------------------------------------------------*/
    local monthNum := {}
    , part := []
    , selectedDate := ''
    , currentDay := ''
    , currentMonth := ''
    , currentYear := ''
    , datePattern := ''
    , currentMonthNum := ""
    /*--------------------------------------------------------------
    Declaration of variables
    --------------------------------------------------------------*/

    monthNum := {
        Jan: "01",
        Feb: "02",
        Mar: "03",
        Mär: "03",
        Apr: "04",
        May: "05",
        Mai: "05",
        Jun: "06",
        Jul: "07",
        Aug: "08",
        Sep: "09",
        Oct: "10",
        Okt: "10",
        Nov: "11",
        Dec: "12",
        Dez: "12"
    }
    ; selectedDate := '29. December 1986'
    ; Copy the selected date safely to A_Clipboard
    selectedDate := SimplyCopy()

    ; split the selcted date in 3 parts and assign accordingly
    part := StrSplit(selectedDate, ' ')
    ; check if German or English
    datePattern := IsNumber(SubStr(part[1], 1, 1)) ? "Deutsch" : "English"

    switch datePattern {
        case "Deutsch":
            currentDay := part[1]
            ; currentDay may be succeeded by an "," or an "."
            ; so let's shorten currentDay by 1
            currentDay := SubStr(currentDay, 1, StrLen(currentDay) - 1)
            ; currentDay may consist of only 1 digit. So let's check the length
            ; of the string: only if it is 1, let's add a "0"
            StrLen(currentDay) = 1 ? currentDay := "0" . currentDay : currentDay
            ; for better readability let's assign 'part' to the appropriate
            ; variables
            currentMonth := part[2]

        case "English":
            ; In the English pattern part[1] will containt the months
            currentMonth := part[1]
            ; In the English pattern part[2] will containt the day
            currentDay := part[2]
            ; currentDay may be succeeded by an "," or an "."
            ; so let's shorten currentDay by 1
            currentDay := SubStr(currentDay, 1, StrLen(currentDay) - 1)
            ; currentDay may consist of only 1 digit. So let's check the length
            ; of the string: only if it is 1, let's add a "0"
            StrLen(currentDay) = 1 ? currentDay := "0" . currentDay : currentDay

    }
    ; currentYear is in both patterns part[3]
    currentYear := part[3]

    ; Get the first 3 characters (month short) of the currentMonth
    currentMonth := SubStr(currentMonth, 1, 3)

    ; Look up the now abbreviated currentMonth in the monthNum-Obj
    currentMonthNum := monthNum.%currentMonth%

    ; Copy the converted date safely to A_Clipboard
    SaveInsert(currentYear . "-" . currentMonthNum . "-" . currentDay)
    toot
}
Below also my present helper file and the UDF-"library" (A few UDF do not make a library, but it is a start and good place for them). Not finished yet. And this last function is not in there either.

Thank you for your help, @mikeyww
Attachments
udf - 2023-08-26 15-53-35.ahk
User Defined Functions
(2.96 KiB) Downloaded 18 times
PrepMediaInfo - 2023-08-26 15-53-35.ahk
Main Helper File
(9.69 KiB) Downloaded 28 times
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b

User avatar
mikeyww
Posts: 27194
Joined: 09 Sep 2014, 18:38

Re: Accessing a very simple object by property variable

26 Aug 2023, 09:31

Yes, you can use a lookup instead of a loop. Take your pick. You won't find any noticeable difference in performance with such a small array, so you can use any preference you have about what is more efficient and why.

Re:

Code: Select all

date := Format('{}-{:02}-{:02.0f}', part[3], moNum(part[2]), part[1])
This assembles the parts in order: yyyy-mm-dd. It ensures that the second part has two digits (leading zero if needed), and ensures that the third part has two digits and no decimal point or any digits that would follow it.

"How are you formatting it?" is answered by the first parameter. "What are you formatting?" is answered by all remaining parameters.

Re:

Code: Select all

If SubStr(moName, 1, 3) ~= 'i)' mo
This conducts a RegExMatch, using the first 3 characters of the month as the haystack, and the array element as the needleRegEx. The match is case-insensitive. If the match succeeds, the value returned will be a positive integer (1 in this case), and the condition of If will then be met.
User avatar
ItisI
Posts: 56
Joined: 03 Jul 2023, 11:50

Re: Accessing a very simple object by property variable

26 Aug 2023, 11:20

Thanks :) A lot :D
Windows 10 - AutoHotkey 2.0.3 - VSCode - AutoHotkey v2 Language Support - vscode-autohotkey-debug

2b || !2b


Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: cgx5871, Draken, teadrinker and 36 guests