Anfängerfragen zum Programmieren mit PureBasic.
mk-soft
Beiträge: 3695 Registriert: 24.11.2004 13:12
Wohnort: Germany
Beitrag
von mk-soft » 08.07.2020 00:21
jacdelad hat geschrieben: Ja, ja, das ist klasse. So klappt das auch mit meiner Map. 2 Fragen sind aber aufgekommen: wie übernehme ich z.B. alle Werte von MeineMap("eins"), ohne gleich die gesamte map zu übernehmen?
Code: Alles auswählen
Structure udtMapData
iVal.i
sVal.s
EndStructure
Global NewMap Map1.udtMapData()
Global NewMap Map2.udtMapData()
Map1("1")\iVal = 100
Map1()\sVal = "Hello World"
Map1("2")\iVal = 200
Map1()\sVal = "PureBasic"
Map1("3")\iVal = 300
Map1()\sVal = "Power"
; Copy Map Elements
Map2("Copy1") = Map1("2")
Map2("Copy2") = Map1("3")
Debug "*** Map1 ***"
ForEach Map1()
Debug Map1()\sVal
Next
Debug "*** Map2 ***"
ForEach Map2()
Debug Map2()\sVal
Next
Jac de Lad
Beiträge: 56 Registriert: 24.07.2019 17:53
Computerausstattung: AMD Ryzen 1700X, MSI B350 PC Mate, 256GB SSD, 16GB RAM@2933MHz, AMD Radeon 7770 OC, 106TB HDD, 28" Samsung 4k-Monitor, Windows 10 x64
Wohnort: Riesa
Kontaktdaten:
Beitrag
von Jac de Lad » 08.07.2020 00:57
Nein ich meinte natürlich in die JSON-Struktur. Oder willst du damit andeuten ich soll jedes einzelne Element in eine temporäre Map kopieren und dann speichern?
mk-soft
Beiträge: 3695 Registriert: 24.11.2004 13:12
Wohnort: Germany
Beitrag
von mk-soft » 08.07.2020 09:34
Ich bin von Deiner Anfrage für das Speichern der ganzen Struktur ausgegangen.
Somit ginge ich aus, das alle Daten die verarbeitet und gesichert werden soll in einer Struktur vorhanden sind.
Dieses macht es auch einfacher ...
Jac de Lad
Beiträge: 56 Registriert: 24.07.2019 17:53
Computerausstattung: AMD Ryzen 1700X, MSI B350 PC Mate, 256GB SSD, 16GB RAM@2933MHz, AMD Radeon 7770 OC, 106TB HDD, 28" Samsung 4k-Monitor, Windows 10 x64
Wohnort: Riesa
Kontaktdaten:
Beitrag
von Jac de Lad » 08.07.2020 11:06
So war es ursprünglich. Eine Map voller bunter Daten. Das klappt auch schon. In einem Teil meines Programms muss ich aber nur einzelne Elemente speichern, also MeineMap ("eins"), MeineMap ("zwei")...jeweils in eine andere Datei.
mk-soft
Beiträge: 3695 Registriert: 24.11.2004 13:12
Wohnort: Germany
Beitrag
von mk-soft » 08.07.2020 17:51
So vielleicht ?
Hatte keine lust auf JSON umzustellen
Code: Alles auswählen
Structure udtMapData
iVal.i
sVal.s
EndStructure
Global NewMap Map1.udtMapData()
Global NewMap Map2.udtMapData()
Map1("1")\iVal = 100
Map1()\sVal = "Hello World"
Map1("2")\iVal = 200
Map1()\sVal = "PureBasic"
Map1("3")\iVal = 300
Map1()\sVal = "Power"
Procedure LoadMapData(FileName.s, *Data.udtMapData)
Protected xml
If FileSize(FileName) > 0
xml = LoadXML(#PB_Any, FileName)
If xml And XMLStatus(xml) = #PB_XML_Success
ExtractXMLStructure(MainXMLNode(xml), *Data, udtMapData, #PB_XML_NoCase)
FreeXML(xml)
ProcedureReturn #True
EndIf
EndIf
ProcedureReturn #False
EndProcedure
; ----
Procedure SaveMapData(FileName.s, *Data.udtMapData)
Protected r1, xml
xml = CreateXML(#PB_Any)
If xml
If InsertXMLStructure(RootXMLNode(xml), *Data, udtMapData)
FormatXML(xml, #PB_XML_ReFormat)
If SaveXML(xml, FileName)
r1 = #True
Else
r1 = #False
EndIf
EndIf
FreeXML(xml)
EndIf
ProcedureReturn r1
EndProcedure
file.s = GetUserDirectory(#PB_Directory_Downloads) + "MapData1.xml"
SaveMapData(file, @Map1("1")) ; <- Adresse auf Map Element
file.s = GetUserDirectory(#PB_Directory_Downloads) + "MapData2.xml"
SaveMapData(file, @Map1("2")) ; <- Adresse auf Map Element
If AddMapElement(Map1(), "100") ; <- Zuerst leere Map Element anlegen
file.s = GetUserDirectory(#PB_Directory_Downloads) + "MapData1.xml"
LoadMapData(file, @Map1())
Debug Map1()\iVal
Debug Map1()\sVal
EndIf
mk-soft
Beiträge: 3695 Registriert: 24.11.2004 13:12
Wohnort: Germany
Beitrag
von mk-soft » 08.07.2020 19:11
P.S.
JSON geht auch sehr gut !
Code: Alles auswählen
Structure udtMapData
iVal.i
sVal.s
List TextList.s()
Map InfoMap.s()
EndStructure
Global NewMap MyMap.udtMapData()
MyMap("1")\iVal = 100
MyMap()\sVal = "Hello World"
For i = 1 To 10
AddElement(MyMap()\TextList())
MyMap()\TextList() = "TextList " + i
Next
MyMap()\InfoMap("DE") = "Deutsch"
MyMap()\InfoMap("EN") = "English"
MyMap("2")\iVal = 200
MyMap()\sVal = "PureBasic"
MyMap("3")\iVal = 300
MyMap()\sVal = "Power"
Procedure LoadMapData(FileName.s, *Data.udtMapData)
Protected json
If FileSize(FileName) > 0
json = LoadJSON(#PB_Any, FileName)
If json
ExtractJSONStructure(JSONValue(json), *Data, udtMapData)
FreeJSON(json)
ProcedureReturn #True
EndIf
EndIf
ProcedureReturn #False
EndProcedure
; ----
Procedure SaveMapData(FileName.s, *Data.udtMapData)
Protected r1, json
json = CreateJSON(#PB_Any)
If json
If #True
InsertJSONStructure(JSONValue(json), *Data, udtMapData)
If SaveJSON(json, FileName, #PB_JSON_PrettyPrint)
r1 = #True
Else
r1 = #False
EndIf
EndIf
FreeJSON(json)
EndIf
ProcedureReturn r1
EndProcedure
file.s = GetUserDirectory(#PB_Directory_Downloads) + "MapData1.json"
SaveMapData(file, @MyMap("1")) ; <- Adresse auf Map Element
file.s = GetUserDirectory(#PB_Directory_Downloads) + "MapData2.json"
SaveMapData(file, @MyMap("2")) ; <- Adresse auf Map Element
*Buffer.udtMapData = AddMapElement(MyMap(), "100") ; <- Zuerst leere Map Element anlegen
If *Buffer
file.s = GetUserDirectory(#PB_Directory_Downloads) + "MapData1.json"
LoadMapData(file, *Buffer)
EndIf
If FindMapElement(MyMap(), "100")
Debug "iVal = " + MyMap()\iVal
Debug "sVal = " + MyMap()\sVal
Debug "TextList ->"
ForEach MyMap()\TextList()
Debug MyMap()\TextList()
Next
Debug "InfoMap ->"
ForEach MyMap()\InfoMap()
Debug MapKey(MyMap()\InfoMap()) + " = " +MyMap()\InfoMap()
Next
EndIf
Jac de Lad
Beiträge: 56 Registriert: 24.07.2019 17:53
Computerausstattung: AMD Ryzen 1700X, MSI B350 PC Mate, 256GB SSD, 16GB RAM@2933MHz, AMD Radeon 7770 OC, 106TB HDD, 28" Samsung 4k-Monitor, Windows 10 x64
Wohnort: Riesa
Kontaktdaten:
Beitrag
von Jac de Lad » 08.07.2020 22:36
Danke, auf die Idee die Adresse mit @ zu holen bin ich natürlich nicht gekommen.