Android Question Help creating a map with a text file

Navierie

New Member
Hi everyone,
I've just started to use b4a because I'm doing a project at university to create and app on android and I'm still very noob with this language so I hope you can help me a little bit.

The problem is I need to create a map from a txt file, but only I need a map from a specific part of the txt file. For example, if this is the txt file:

//////////////////////////////////
[General]
name = TCM

;11 bits identifiers (standard identifiers)
[std_iden]
;request identifier
rq_iden = 0x7E1
;transmission identifier
tx_iden = 0x7E9

;29 bits identifiers (extended identifiers)
[ext_iden]
;request identifier
rq_iden = 0x18DA18F1
;transmission identifier
tx_iden = 0x18DAF118

[DTCs]
DTC1 = P0101


[PID1]
Type = Variable
Number = 5
Name = Engine Coolant Temp.
Units =ºC
MinRaw = 0
MaxRaw = 255
MinEng = -40
MaxEng = 215
Nbytes = 1
Formula = A-40

[PID2]
Type = Variable
Number = 0x0C
Name = Engine rpm
Units = rpm
MinRaw = 0
MaxRaw = 65535
MinEng = 0
MaxEng = 16383.75
Nbytes = 2
Formula = (A*256+B)/4
////////////////////////////

And I need to create a map of what's written under PID1 and above PID2, how can I do that?
Thank you very much for your help
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim data As Map
data.Initialize
For Each l As String In File.ReadList(File.DirAssets, "1.txt")
   If l.StartsWith("/") Or l.StartsWith(";") Or l.Trim.Length = 0 Then Continue
   Dim m As Matcher = Regex.Matcher("\[(.*)\]", l)
   If m.Find Then
     Dim currentItems As Map
     currentItems.Initialize
     data.Put(m.Group(1), currentItems)
   Else
     m = Regex.Matcher("(\w+)\s*=\s*(.*)", l)
     If m.Find Then
       currentItems.Put(m.Group(1), m.Group(2))
     Else
       Log("skipping: " & l)
     End If
   End If
Next
Log(data)
Dim mp As Map = data.Get("PID1")
Log(mp.Get("MinEng"))
 
  • Like
Reactions: udg
Upvote 0

Navierie

New Member
B4X:
Dim data As Map
data.Initialize
For Each l As String In File.ReadList(File.DirAssets, "1.txt")
   If l.StartsWith("/") Or l.StartsWith(";") Or l.Trim.Length = 0 Then Continue
   Dim m As Matcher = Regex.Matcher("\[(.*)\]", l)
   If m.Find Then
     Dim currentItems As Map
     currentItems.Initialize
     data.Put(m.Group(1), currentItems)
   Else
     m = Regex.Matcher("(\w+)\s*=\s*(.*)", l)
     If m.Find Then
       currentItems.Put(m.Group(1), m.Group(2))
     Else
       Log("skipping: " & l)
     End If
   End If
Next
Log(data)
Dim mp As Map = data.Get("PID1")
Log(mp.Get("MinEng"))
Hi!
Thank you for your quick answer Erel. Can I ask you a favour? Could you please explain me what does every line of these code? I'm really noob with b4a and I need to understand what did you do. Thank you very much.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I recommend you to start with the beginner's guide

Yes, but your code is very advanced, dear Erel. Even I need to take a deeper look, what it's doing :)

@Navierie : Do you have an example how the map should look like? It's all about adding Keys/Values at the right position (even a map itsself can be added as a value in another map).
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
To help better understand what is happening I recommend using one of the Regular Expression testers that are available online. Just paste the text into the tester and add the expression Erel has used and it will highlight the group's of text that are found.
 
Upvote 0
Top