Forenindex » Programmierung/Entwicklung » AppleScript » Liste in Liste vergleichen?

Liste in Liste vergleichen?

NEU!
Beiträge gesamt: 180

8. Sep 2014, 09:04
Bewertung:

gelesen: 1816

Beitrag als Lesezeichen
Hallo zusammen,
Ich habe mal ein Beispiel-Script geschrieben und ich verstehe eine Sache nicht: Warum kann AS den Inhalt einer Liste in einer Liste nicht vernünftig vergleichen, wie geht das unkompliziert besser bzw überhaupt?

Eigentlich dürfte "a2" nicht zweimal im Ergebnis auftauchen, und wenn ich keine Liste in einer Liste habe, sondern zB nur "2", dann funktioniert das if ... Is not in ... then auch gut.
Wer kann mir helfen bitte?




set tempList to {"1", {"a", "2"}, "3", "4", "5"}
set theList to {{"a", "2"}}

repeat with k from 1 to length of tempList
if item k of tempList is not in theList then
copy item k of tempList to the end of theList
end if
end repeat

set theText to ""
repeat with f from 1 to length of theList
set theText to theText & item f of theList & linefeed
end repeat

display dialog "" & theText

Ergebnis:
"a2
1
a2
3
4
5
"

Liste in Liste vergleichen?

Hans Haesler
  
Beiträge gesamt: 5826

8. Sep 2014, 11:33
Bewertung:

gelesen: 1784

Beitrag als Lesezeichen
Hallo NEU!,

eine kleine Änderung – und schon funktioniert es wie erwartet:

Code
set tempList to {"1", {"a", "2"}, "3", "4", "5"} 
set theList to {{"a", "2"}}

repeat with k from 1 to length of tempList
if {item k of tempList} is not in theList then
copy item k of tempList to the end of theList
end if
end repeat

set theText to ""
repeat with f from 1 to length of theList
set theText to theText & item f of theList & linefeed
end repeat

display dialog "" & theText

Damit nicht zwei Mal auf 'tempList' zugegriffen wird: Das aktuelle Element einer Variablen zuweisen.
Und um zu vermeiden, dass 'theText' bei jedem Umgang dupliziert wird: Das Vorbereiten des Ergebnis-Textes zunächst als Liste vornehmen.

Code
set tempList to {"1", {"a", "2"}, "3", "4", "5"} 
set theList to {{"a", "2"}}

repeat with k from 1 to length of tempList
set curItem to item k of tempList
if {curItem} is not in theList then
copy curItem to the end of theList
end if
end repeat

set theText to item 1 of theList
repeat with f from 2 to length of theList
copy (return & item f of theList) to the end of theText
end repeat
set theText to (theText as string)

display dialog theText

Gruss, Hans

Liste in Liste vergleichen?

NEU!
Beiträge gesamt: 180

8. Sep 2014, 11:56
Bewertung:

gelesen: 1769

Beitrag als Lesezeichen
Prima, dann war es ja quasi nur {currItem} in geschweifte Klammern zu setzten :-)

Die Ausgabe von theText war übrigens nur zur Visualisierung, hätte auch für mich log theList nehmen könne, aber danke dass du dich da auch drüber hergemacht hast.

Ich freue mich dass du mir geholfen hast,
einen schönen Start in die Woche noch,
Jan