Seite 2 von 2

Re: Wie speicher ich eine komplexere Struktur?

Verfasst: 08.07.2020 00:21
von mk-soft
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

Re: Wie speicher ich eine komplexere Struktur?

Verfasst: 08.07.2020 00:57
von Jac de Lad
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?

Re: Wie speicher ich eine komplexere Struktur?

Verfasst: 08.07.2020 09:34
von mk-soft
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 ...

Re: Wie speicher ich eine komplexere Struktur?

Verfasst: 08.07.2020 11:06
von Jac de Lad
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.

Re: Wie speicher ich eine komplexere Struktur?

Verfasst: 08.07.2020 17:51
von mk-soft
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

Re: Wie speicher ich eine komplexere Struktur?

Verfasst: 08.07.2020 19:11
von mk-soft
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

Re: Wie speicher ich eine komplexere Struktur?

Verfasst: 08.07.2020 22:36
von Jac de Lad
Danke, auf die Idee die Adresse mit @ zu holen bin ich natürlich nicht gekommen.