Variable in Array suchen und Position ausgeben Topic is solved

Stelle Fragen zur Programmierung mit Autohotkey

Moderator: jNizM

Wick3d
Posts: 31
Joined: 04 Apr 2014, 03:24

Variable in Array suchen und Position ausgeben

Post by Wick3d » 16 Oct 2020, 08:38

Hallo Leute,

Ich bin mir sicher, mein Problem ist ganz leicht. Dennoch finde ich nach langer Suche nirgendwo eine schöne Lösung.
Ich möchte anhand einer Variable herausfinden, ob das Wort, welches in der Variable ist, im Array enthalten ist und wenn ja die Position ausgeben.

Ungefähr so sollte es dann aussehen

Code: Select all

arr := ["a", "ll", "", "d"]
var := "ll"
MsgBox % arr[var] ; gibt 2 wieder
Ich bin eigentlich der Meinung, dass ich sowas schon gemacht habe. Aber irgendwie finde ich es nicht mehr :crazy:

Danke für eure Hilfe.

Gruß
Alex :beard:

User avatar
Ragnar
Posts: 614
Joined: 30 Sep 2013, 15:25

Re: Variable in Array suchen und Position ausgeben  Topic is solved

Post by Ragnar » 16 Oct 2020, 09:16

Code: Select all

obj := {"a": 1, "ll": 2, "": 3, "d": 4}
var := "ll"
MsgBox % obj[var] ; gibt 2 wieder

Wick3d
Posts: 31
Joined: 04 Apr 2014, 03:24

Re: Variable in Array suchen und Position ausgeben

Post by Wick3d » 16 Oct 2020, 10:55

Oh man, ich wusste ich bin nah dran. Danke. :D

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Variable in Array suchen und Position ausgeben

Post by BoBo » 16 Oct 2020, 19:16

Code: Select all

arr := ["a", "ll", "", "d"], var := "ll"
Loop 
	{ pos := A_Index
	} Until var = arr[pos]
MsgBox % pos
Geht auch mit nicht-assoziativem Array.

effel
Posts: 546
Joined: 16 Jan 2018, 13:34

Re: Variable in Array suchen und Position ausgeben

Post by effel » 16 Oct 2020, 19:59

BoBo wrote:
16 Oct 2020, 19:16
Danke BoBo das kann ich brauchen.

Code: Select all

arr := ["a", "ll", "https://www.autohotkey.com/boards/viewtopic.php?p=358377", "d"]

var := "https://www.autohotkey.com/boards/viewtopic.php?p=358377"

Loop
{pos := A_Index 
}Until var = arr[pos]

MsgBox % "Position:`n" pos "`n`nWert:`n" arr[pos]

Code: Select all

arr := ["a", "ll", "https://www.autohotkey.com/boards/viewtopic.php?p=358377", "d"]

var := "https://www.autohotkey.com/boards/viewtopic.php?p=358377"

MsgBox % FindString(Arr, Var)

FindString(Arr, Var){
Loop
{pos := A_Index 
}Until Var = Arr[pos]
Return "Position:`n" pos "`n`nWert:`n" arr[pos]
}

Hier finde ich jedoch nur das erste Vorkommen:

Code: Select all

arr := ["a", "ll", "https://www.autohotkey.com/boards/viewtopic.php?p=358377", "d"]

var := "autohotkey"

MsgBox % FindTeilString(Arr, Var)

FindTeilString(Arr, Var){
Loop
{pos := A_Index 
}Until InStr(Arr[pos],Var)
Return "Position:`n" pos "`n`nWert:`n" arr[pos]
}
Das funktioniert dann doch nicht wie gewünscht 😊 auch hier wird nur der erste Fund angezeigt

Code: Select all

arr := ["autohotkey", "ll", "https://www.autohotkey.com/boards/viewtopic.php?p=358377", "d"]

var := "autohotkey"

MsgBox % FindString(Arr, Var)

FindString(Arr, Var){
Loop
{pos := A_Index 
}Until InStr(Arr[pos],Var)
NeedLes .= "Position:`n" pos "`n`nWert:`n" arr[pos]
Return NeedLes
}

just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Variable in Array suchen und Position ausgeben

Post by just me » 17 Oct 2020, 02:50

Böse Falle!

Code: Select all

arr := ["a", "ll", "", "d"]
var := "Gibbetnich!"
Loop 
	{ pos := A_Index
	} Until var = arr[pos]
MsgBox % pos
;)

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Variable in Array suchen und Position ausgeben

Post by haichen » 17 Oct 2020, 04:12

:o Die böse Falle hatte ich auch nicht gesehen..
Das hier geht:

Code: Select all

arr := ["a", "ll", "", "d"]
var := "Gibbetnich!"
Loop {
 } Until pos:=A_Index*((var = arr[A_Index] ) or (A_Index = arr.MaxIndex()))
MsgBox % pos

effel
Posts: 546
Joined: 16 Jan 2018, 13:34

Re: Variable in Array suchen und Position ausgeben

Post by effel » 17 Oct 2020, 11:28

Code: Select all

arr := ["a", "ll", "d"]
maxIndex := arr.Push("endOfFile")
var := "d "
Loop {
 } Until pos:=A_Index*((var = arr[A_Index] ) or (A_Index = arr.MaxIndex()))
NeedLes := "Position:`n" pos "`n`nWert:`n" arr[pos]
if InStr(NeedLes,"endOfFile")
MsgBox % --maxIndex " Werte durchsucht. Gibbetnich!"
else
MsgBox % NeedLes 


BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Variable in Array suchen und Position ausgeben

Post by BoBo » 17 Oct 2020, 13:36

<See below>

Wick3d
Posts: 31
Joined: 04 Apr 2014, 03:24

Re: Variable in Array suchen und Position ausgeben

Post by Wick3d » 17 Oct 2020, 13:46

Hi Bobo,

falschen Thread erwischt? Ich mein die Inhalte vom Code ähneln einem anderen Thread von mir. Ich erkläre den Sinn gerne erneut ;)
https://www.autohotkey.com/boards/viewtopic.php?f=9&t=82186
Last edited by BoBo on 17 Oct 2020, 14:05, edited 1 time in total.
Reason: Moderatorenkonfusion & korrekter Einwand. BoBo hat sein Posting an den eingefügten Link verschoben. Rgds, BoBo.

just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Variable in Array suchen und Position ausgeben

Post by just me » 18 Oct 2020, 05:08

Moin, noch zwei:

Code: Select all

Arr := ["a", "ll", "d"]
Var := "d "
FoundPos := 0
; 1. --------------------------------------------------
Loop, % Arr.Length() {
} Until (Var = Arr[A_Index]) && (Foundpos := A_Index)
; 2. --------------------------------------------------
For I, V In Arr {
} Until (Var = V) && (FoundPos := I)
; -----------------------------------------------------
If (FoundPos = 0)
   MsgBox % Arr.Length() " Werte durchsucht. Gibbetnich!"
Else
   MsgBox % "Position:`n" FoundPos "`n`nWert:`n" Arr[FoundPos]
Die Until Prüfung enthält einen 'schmutzigen' oder auch 'genialen' Trick. (FoundPos := I) wird als zweite Bedingung nur dann ausgeführt und geprüft, wenn (Var = V) wahr ist.

Edit: 2020-10-21 Skript korrigiert!
Last edited by just me on 21 Oct 2020, 03:48, edited 1 time in total.

Rohwedder
Posts: 7645
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Variable in Array suchen und Position ausgeben

Post by Rohwedder » 20 Oct 2020, 10:12

Hallo,
ist meinem Autohotkey zu schmutzig, obwohl es meinen Kode gewohnt ist.
Error: Unexpected "{"
---> 011: For I,V in Arr {If (FoundPos = 0)

War es so gemeint?:

Code: Select all

Arr := ["a", "ll", "d"]
Var := "d "
FoundPos := 0
; 1. --------------------------------------------------
Loop, % Arr.Length() {
} Until (Var = Arr[A_Index]) && (Foundpos := A_Index)
; 2. --------------------------------------------------
For I, V In Arr {
} Until (Var = V) && (FoundPos := I)
; -----------------------------------------------------
For I, V In Arr {
} If (FoundPos = 0)
   MsgBox % Arr.Length() " Werte durchsucht. Gibbetnich!"
Else
   MsgBox % "Position:`n" FoundPos "`n`nWert:`n" Arr[FoundPos]

effel
Posts: 546
Joined: 16 Jan 2018, 13:34

Re: Variable in Array suchen und Position ausgeben

Post by effel » 20 Oct 2020, 10:24

Eines der wenigen laufenden Scripte bei mir 😊
2020-10-20 at 17.22.17.jpeg
(86.63 KiB) Downloaded 37 times

just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Variable in Array suchen und Position ausgeben

Post by just me » 21 Oct 2020, 03:51

Nöh, die letzte For-Schleife war ein schlichter Kopierfehler. Bei mir läuft das aber trotzdem. Ich habe das Skript oben korrigiert.

Post Reply

Return to “Ich brauche Hilfe”