newbe coding questions

Just starting out? Need help? Post your questions and find answers here.
charles
New User
New User
Posts: 2
Joined: Thu Jul 20, 2017 6:02 pm

newbe coding questions

Post by charles »

Currently, my biggest problem in trying to understand how to use PureBasic, is INPUT and OUTPUT related. The programs I create are heavy numerical data crunchers and not much of the other “fancier” stuff. There does not seem to be much in the documentation about this for the Mac.

Right now I am trying to find lines of code in PureBasic that will:

1) Set up a simple computer screen window then print a “request” for some numerical data (like “ Your LV Weight is … “ ) on the screen and wait for the user to input that number from the key board, and display that imput on the screen.

2) Open a numerical data file and read the data into the program. Some times data file will have 1 number on a line in the file ( like Read A ) and other times there will be several numbers on each line of data file ( like Read B, C, D, E ).

3) Set up so I can print a table of data output via a printer.

These sound like they should be pretty simple but I have not been able to find PureBasic code examples that will do these tasks.

If you can give me sample code that will do the above, it would be greatly appreciated. Either here or Charles.Meyers2@yahoo

Thanks
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: newbe coding questions

Post by Little John »

charles wrote:1) Set up a simple computer screen window then print a “request” for some numerical data (like “ Your LV Weight is … “ ) on the screen and wait for the user to input that number from the key board, and display that imput on the screen.
Run the following code, enter something in the white field, then click at the OK button.

Code: Select all

EnableExplicit

; Windows
Enumeration
   #WinMain
EndEnumeration

; Gadgets
Enumeration
   #GadPrompt
   #GadInput
   #GadResult
   #GadBtnOK
EndEnumeration

Define event.i

If OpenWindow(#WinMain, 0, 0, 200, 110, "") = 0
   MessageRequester("Fatal error", "Can't open main window.")
   End
EndIf
TextGadget(#GadPrompt, 10, 15, 90, 20, "Your LV Weight is:")
StringGadget(#GadInput, 110, 12, 50, 20, "")
TextGadget(#GadResult, 10, 45, 120, 20, "")
ButtonGadget(#GadBtnOK, 70, 75, 60, 25, "OK")

Repeat
   event = WaitWindowEvent()
   
   Select event
      Case #PB_Event_Gadget
         Select EventGadget()
            Case #GadBtnOK
               SetGadgetText(#GadResult, "You entered " + GetGadgetText(#GadInput))
         EndSelect     
   EndSelect      
Until event = #PB_Event_CloseWindow
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: newbe coding questions

Post by IdeasVacuum »

Welcome to PB Charles

Are you a newbie to PB or to coding?

1a) Open a Window and place either a TextGadget (User can't edit) or String Gadget (User can edit)
http://www.purebasic.com/documentation/ ... indow.html
http://www.purebasic.com/documentation/ ... adget.html
http://www.purebasic.com/documentation/ ... adget.html
...add a button to get things rolling: http://www.purebasic.com/documentation/ ... adget.html

1b) Ask your User for info with a requester.
http://www.purebasic.com/documentation/ ... ester.html
Or simply present your Window as a Form to fill-in.

2) You can read-in a file, replacing the seperator (e.g. a comma) with a Table (ListIconGadget) seperator (#LF$)
http://www.purebasic.com/documentation/ ... dfile.html
http://www.purebasic.com/documentation/ ... tring.html
http://www.purebasic.com/documentation/ ... adget.html

3) Slightly more complicated, because you would need to draw the table programmatically before printing. Normally I avoid this and define a PDF Table instead, printing (if required), via the PDF reader app.

Code: Select all

EnableExplicit

Enumeration
#MyWindow
#LabelUserName
#StrUserName
#LabelLvWeight
#StrLvWeight
#BtnStart
#Font12
EndEnumeration

Global igFont12.i = LoadFont(#Font12, "Arial", 12, #PB_Font_HighQuality)

Procedure AskUser()
;#-----------------
Protected sInput.s

              sInput = InputRequester("Name", "Enter Your Name", "")
                        SetGadgetText(#StrUserName, sInput)

              sInput = InputRequester("Weight", "Enter Your LV Weight", "")
                        SetGadgetText(#StrLvWeight, sInput)
EndProcedure

Procedure MyWindow()
;#------------------
Protected iFlags.i = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered

              If OpenWindow(#MyWindow, 0, 0, 412, 212, "Click Start!", iFlags)

                      SetGadgetFont(#PB_Default, igFont12)

                         TextGadget(#LabelUserName, 20,  28, 374, 16, "Your Name")
                       StringGadget(#StrUserName,   20,  46, 374, 34, "")

                         TextGadget(#LabelLvWeight, 20, 100, 374, 16, "LV Weight")
                       StringGadget(#StrLvWeight,   20, 118, 374, 34, "")

                       ButtonGadget(#BtnStart,     110, 168, 186, 30, "START")
              EndIf
EndProcedure

Procedure WaitForUser()
;#---------------------
Protected iExit.i = #False

              Repeat

                    Select WaitWindowEvent(1)

                               Case #PB_Event_CloseWindow: iExit = #True
                               Case #PB_Event_Gadget

                                          Select EventGadget()

                                                     Case  #BtnStart: AskUser()
                                          EndSelect
                    EndSelect

              Until iExit = #True
EndProcedure

   MyWindow()
WaitForUser()
End
[/size]
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: newbe coding questions

Post by IdeasVacuum »

Hi again

A crude file-in/file out example:

Sample File download: SampleFile.csv

Code: Select all

EnableExplicit

Enumeration
#MyList
#StrInputFile
#FileIO
#StrOutputFile
#MyWindow
#LabelInputFile
#BtnInputFile
#LabelOutPutFolder
#BtnOutputFile
#BtnFillTable
#BtnSaveData
#Font12
EndEnumeration

Structure Vals
sName.s
dWght.d
EndStructure

Global NewList gMyVals.Vals()

Global igFont12.i = LoadFont(#Font12, "Arial", 12, #PB_Font_HighQuality)

Declare SaveDataFile()
Declare PopulateTable()
Declare LoadDataIn()
Declare GetFileIn()
Declare GetPathOut()
Declare MyWindow()
Declare WaitForUser()


Procedure SaveDataFile()
;#----------------------
Protected sOutputFile.s = GetGadgetText(#StrOutputFile)
Protected sRow.s

              If CreateFile(#FileIO, sOutputFile)

                      WriteStringFormat(#FileIO, #PB_UTF8)

                      ForEach gMyVals()

                              sRow = gMyVals()\sName + "," + StrD(gMyVals()\dWght, 2)

                              WriteStringN(#FileIO, sRow)
                      Next

                      CloseFile(#FileIO)
              EndIf
EndProcedure

Procedure PopulateTable()
;#-----------------------
Protected sRow.s
Protected iCnt.i = 0

              ForEach gMyVals()

                      sRow = RSet(Str(iCnt), 3, "0") + #LF$ + gMyVals()\sName + #LF$ + StrD(gMyVals()\dWght, 2)
                      icnt = iCnt + 1

                      AddGadgetItem(#MyList, -1, sRow)
              Next
EndProcedure

Procedure LoadDataIn()
;#--------------------
Protected sInputFile.s = GetGadgetText(#StrInputFile)
Protected sDelimiter.s = ","
Protected       sVal.s
Protected    iFormat.i

              If(FileSize(sInputFile) > 0)

                      If ReadFile(#FileIO, sInputFile)

                               iFormat = ReadStringFormat(#FileIO)

                               While Eof(#FileIO) = 0

                                        AddElement(gMyVals())

                                                   sVal = ReadString(#FileIO, iFormat)
                                        gMyVals()\sName = StringField(sVal, 1, sDelimiter)
                                        gMyVals()\dWght = ValD(StringField(sVal, 2, sDelimiter))
                               Wend

                               CloseFile(#FileIO)
                      EndIf
              Else
                      MessageRequester("Input File", "File is empty?")
              EndIf
EndProcedure

Procedure GetFileIn()
;#-------------------
Protected sInputFile.s = OpenFileRequester("Input File: Select File", "", "All Supported File Types *.* | *.CSV; *.csv; *.TXT; *.txt; | CSV *.csv| *.CSV; *.csv | Text *.txt| *.TXT; *.txt", 0)

              If(FileSize(sInputFile) > 0)

                         SetGadgetText(#StrInputFile, sInputFile)
              Else
                      MessageRequester("Input File", "A file was not selected")
              EndIf
EndProcedure

Procedure GetPathOut()
;#--------------------
Protected sCurrOutputFile.s, sFileName.s, sFileExt.s, sFilePathIn.s, sFilePathOut.s
Protected    sFileOutDate.s = FormatDate("%yyyy_%mm_%dd", Date())

                   sFilePathIn = GetPathPart(GetGadgetText(#StrInputFile))
                  sFilePathOut = PathRequester("OutPut Folder: Select Output File Folder", sFilePathIn)

              If(sFilePathOut)
                                    sFileName = GetFilePart(GetGadgetText(#StrInputFile), #PB_FileSystem_NoExtension)
                                     sFileExt = GetExtensionPart(GetGadgetText(#StrInputFile))
                              sCurrOutputFile = sFilePathOut + sFileName + "_" + sFileOutDate + "." + sFileExt

                                 SetGadgetText(#StrOutputFile, sCurrOutputFile)
              Else
                              MessageRequester("Output Folder", "An Output File Folder was not selected")
              EndIf
EndProcedure

Procedure MyWindow()
;#------------------
Protected iFlags.i = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered

              If OpenWindow(#MyWindow, 0, 0, 530, 340, "Load File, Save File", iFlags)

                        SetGadgetFont(#PB_Default, igFont12)

                           TextGadget(#LabelInputFile,     20,  16, 400, 20, "Input File")
                         StringGadget(#StrInputFile,       20,  38, 400, 34, "")
                         ButtonGadget(#BtnInputFile,      422,  38,  90, 34, "Get File")

                           TextGadget(#LabelOutPutFolder,  20,  76, 400, 20, "Output File")
                         StringGadget(#StrOutputFile,      20,  98, 400, 34, "")
                         ButtonGadget(#BtnOutputFile,     422,  98,  90, 34, "Get Folder")

                         ButtonGadget(#BtnFillTable,       20, 140, 200, 30, "Populate Table")
                         ButtonGadget(#BtnSaveData,       220, 140, 200, 30, "Save Data")

                       ListIconGadget(#MyList,             20, 180, 400, 150, "Num", 60, #PB_ListIcon_FullRowSelect | #PB_ListIcon_GridLines)
                      AddGadgetColumn(#MyList, 1, "Name", 200)
                      AddGadgetColumn(#MyList, 2, "LV Weight kg", 120)
              EndIf
EndProcedure

Procedure WaitForUser()
;#---------------------
Protected iExit.i = #False

              Repeat

                    Select WaitWindowEvent(1)

                               Case #PB_Event_CloseWindow: iExit = #True

                               Case #PB_Event_Gadget

                                          Select EventGadget()

                                                     Case  #BtnInputFile: GetFileIn()
                                                     Case #BtnOutputFile: GetPathOut()
                                                     Case  #BtnFillTable: LoadDataIn() : PopulateTable()
                                                     Case   #BtnSaveData: SaveDataFile()
                                          EndSelect
                    EndSelect

              Until iExit = #True
EndProcedure

   MyWindow()
WaitForUser()
End
[/size]
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: newbe coding questions

Post by IdeasVacuum »

Code to print the content of a ListIcon (Table):
http://www.purebasic.fr/english/viewtop ... 12&t=21642
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: newbe coding questions

Post by blueb »

charles wrote:Currently, my biggest problem in trying to understand how to use PureBasic, is INPUT and OUTPUT related. The programs I create are heavy numerical data crunchers and not much of the other “fancier” stuff. There does not seem to be much in the documentation about this for the Mac.

These sound like they should be pretty simple but I have not been able to find PureBasic code examples that will do these tasks.
For numeric code, etc., I suggest the PureBasic code at the RosettaCode web site (it's maintained by some very good PB coders). There are 539 code samples at this site that will help you.

Site: http://rosettacode.org/wiki/Category:PureBasic
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
charles
New User
New User
Posts: 2
Joined: Thu Jul 20, 2017 6:02 pm

Re: newbie coding questions

Post by charles »

Sorry it took so long to get back to you all. The notifications of your answers went to my SPAM folder and I just saw them.

Thanks to those who responded. I will be trying those solutions. I have done "technical civil engineering and aerospace engineering" problem solving in Fortran (starting in college with Fortran II in 1962) but they had relatively simple input and output. They were very limited in using the fancier stuff now available in PureBASIC. So, it will take some time to understand and use your suggestions as part of my "retirement hobby".

Thanks again.

Charles
Post Reply