How to print row with separators

General FreeBASIC programming questions.
Ichibane
Posts: 2
Joined: Oct 16, 2018 4:33

How to print row with separators

Postby Ichibane » Jan 10, 2019 7:05

I would like to ask on how can i separate one row data with separator

20180001 | Christian | Cortez | BSIT

into

20180001
Christian
Cortez
BSIT

Thanks.
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: How to print row with separators

Postby D.J.Peters » Jan 10, 2019 10:18

Code: Select all

#include "crt.bi"
var s ="20180001 | Christian |Cortez|BSIT", d = " |"
var p = strtok(s,d)
while(p) : print *p :   p = strtok(0,d) : wend
print

s = "20180001|Christian Cortez;BSIT": d = "| ;"
p = strtok(s,d)
while(p) : print *p : p = strtok(0,d) : wend
sleep
oyster
Posts: 179
Joined: Oct 11, 2005 10:46

Re: How to print row with separators

Postby oyster » Jan 10, 2019 12:56

or you can use the function supplied by v1ctor
viewtopic.php?t=130
UEZ
Posts: 312
Joined: May 05, 2017 19:59
Location: Germany

Re: How to print row with separators

Postby UEZ » Jan 10, 2019 22:12

Here another variant:

Code: Select all

'Coded by UEZ
'libpcre.a needed in lib folder!
#Ifdef __Fb_64bit__
   #Libpath "lib\win64\" 'doesn't work!
#Else
   #Libpath "lib\win32\"
#Endif

#Define PCRE_STATIC
#Include "pcre.bi"

Function RegEx(Byval aPattern As String, Byval aSubject As String, aArr() As String) As Integer
  Const OVECCOUNT = 300
 
  Dim As Zstring Ptr error_
  Dim As Integer error_offset, rc, i, ovector(OVECCOUNT - 1), result
  Dim As pcre Ptr re
 
  Erase aArr
  result = 0
 
  re = pcre_compile(aPattern, 0, @error_, @error_offset, NULL)
  If re = NULL Then
    Return result
  End If
 
  i = 0
  Do
    rc = pcre_exec(re, NULL, Strptr(aSubject), Len(aSubject), i, 0, @ovector(0), OVECCOUNT)
    If rc > 0 Then
      Redim Preserve aArr(Lbound(aArr) To Ubound(aArr) + 1)
      aArr(Ubound(aArr)) = Mid(aSubject, ovector(0) + 1, ovector(1) - ovector(0))
      result += 1
      i = ovector(1)
    End If
  Loop While rc >= 0

  Return result
End Function

Function ArrayToString(aArr() As String) as String
   Dim As String sResult
   For i As Integer = Lbound(aArr) To Ubound(aArr)
      sResult &= aArr(i) & Chr(10, 13)
   Next i
   Return sResult
End Function

Function StringSplit(sInput As String, sSeperator As String, bCaseSensitive As Boolean = False) As String
   Dim As String aResult()
   If bCaseSensitive Then
      If RegEx("[^" & sSeperator & "]+", sInput, aResult()) = 0 Then Return "0"
   Else
      If RegEx("(?i)[^" & sSeperator & "]+", sInput, aResult()) = 0 Then Return "0"
   Endif
   Return ArrayToString(aResult())
End Function

Dim As String sString = "20180001 | Christian | Cortez0 | BSIT"
? "Input string: " & sString
?
? "Splitting at "" | "":"
? StringSplit(sString, " | ")
?
? "Splitting at 0:"
? StringSplit(sString, "0")
?
? "Splitting at S (case sensitive):"
? StringSplit(sString, "S", True)

Sleep



libpcre.a can be found here: viewtopic.php?f=2&t=25070&p=224823&hilit=libpcre#p224817.

Output should look like this here:

Input string: 20180001 | Christian | Cortez0 | BSIT

Splitting at " | ":
20180001
Christian
Cortez0
BSIT


Splitting at 0:
2
18
1 | Christian | Cortez
| BSIT


Splitting at S (case sensitive):
20180001 | Christian | Cortez0 | B
IT

Ichibane
Posts: 2
Joined: Oct 16, 2018 4:33

Re: How to print row with separators

Postby Ichibane » Jan 11, 2019 9:20

Thank you for your answers :)

Here is my source code now and I'm happy that it's working :)

Code: Select all

#include "vbcompat.bi" 'For Format
#include "crt.bi"

Declare Sub emp_add()
Declare Sub emp_search()
Declare Sub emp_list()

Type Employee
   id As String
   last_name As String
   first_name As String
   middle_name As String
   gender As String
End Type

Dim As Integer x,y,button,sh,sw

sh = 400 'Screen Height
sw = 600 'Screen Width

ScreenRes(sw,sh)

Do
   Dim d As Double = Now()
   Locate(49,45)
   Print Format(d, "mmmm dd, yyyy - h:mm:ss AM/PM")
   GetMouse(x,y,,button)
   Locate(1,30)
   Print "Payroll System"
   Line (1, 40)-(150, 70),,b
   Draw String(10,50), "Add Employee"
   Line (1, 90)-(150, 120),,b
   Draw String(10,100), "Search Employee"
   Line (1, 140)-(150, 170),,b
   Draw String(10,150), "View All Employee"
   Line (1, 190)-(150, 220),,b
   Draw String(10,200), "Exit"
   
   If x>=1 And x<=150 And button=1 Then
      While InKey <> "": Wend
      If y>=40 And y<=70 Then
         Line (1, 40)-(150, 70),1,bf
         Draw String(10,50), "Add Employee"
         Sleep 300
         Cls
         emp_add()
      ElseIf y>=90 And y<=120 Then
         Line (1, 90)-(150, 120),1,bf
         Draw String(10,100), "Search Employee"
         Sleep 300
         Cls
         emp_search()
      ElseIf y>=140 And y<=170 Then
         Line (1, 140)-(150, 170),1,bf
         Draw String(10,150), "View All Employee"
         Sleep 300
         Cls
         emp_list()
      ElseIf y>=190 And y<=220 Then
         Line (1, 190)-(150, 220),1,bf
         Draw String(10,200), "Exit"
         Sleep 300
         End
      EndIf
   EndIf
Loop

Sub emp_add()
   Dim d As Double = Now
   Dim As Integer count = 1, found = 0
   Dim temp As String
   Dim tempId As Integer
   Dim array(1 To 5) As String = {"Employee ID"," Last Name","First Name","Middle Name","Gender"}
   Dim emp As Employee
   
   'For EmployeeID
   Open "employees.txt" For Input As #1
      While Not Eof(1)
         Input #1, temp
         count+=1
      Wend
   Close #1
   
   tempId = Val(Format(d,"yyyy0000")) + count 'Temporary EmployeeID
   emp.id = Trim(Str(tempId)) 'Converting tempID to String and removing whitespaces using Trim
   
   Print
   Print "Add Employee"
   Print
   Print "Employee ID: " & emp.id
   Input "Last Name: ", emp.last_name
   Input "First Name: ", emp.first_name
   Input "Middle Name: ", emp.middle_name
   Input "Gender: ", emp.gender
   
   Open "employees.txt" For Append As #1
      Print #1, emp.id & " | " & emp.last_name & " | " & emp.first_name & " | " & emp.middle_name & " | " & emp.gender
   Close #1
   
   Print
   Print "Employee successfully added"
   Sleep
   Cls
End Sub

Sub emp_search()
   'No codes yet
End Sub

Sub emp_list()
   Dim emp(0) As Employee
   Dim temp As String
   Dim i As Integer
   Dim array(1 To 5) As String = {"Employee ID"," Last Name","First Name","Middle Name","Gender"}
   Open "employees.txt" For Input As #1
      While Not Eof(1)
         Input #1, temp
         Var d = "|"
         Var p = strtok(temp,d)
         i=0
         While(p)
            i+=1
            Print Trim(array(i)) & ": " & Trim(*p)
            p = strtok(0,d)
            If i=5 Then Exit While
         Wend
         Print
         Print "------------"
         Print
      Wend
   Close #1
   Sleep
   Cls
End Sub
jfiofficial
Posts: 1
Joined: Nov 05, 2018 8:55

Re: How to print row with separators

Postby jfiofficial » Jan 11, 2019 9:23

galing mo po :)

Return to “General”

Who is online

Users browsing this forum: Bing [Bot], integer and 3 guests