I need another set of eyes , mine not working so good .

Just starting out? Need help? Post your questions and find answers here.
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

I need another set of eyes , mine not working so good .

Post by vmars316 »

Hello & Thanks ;
I can't figure this out :
Program :
Reads in an .html file one line at time into OneLine$ .
Appends OneLine$ at a time into OutputString$ .
Reads OutputString$ and Inserts a Chr(13) after each ">" .
Writes each Line into EditorArea .
Then writes each line into "HtmlLinesSplitAtGT.html" .

The problem is that is doesn't write out the last 3 lines ,
<div id="bg_popup-container"></div>
</body>
</html>

Thanks for your help .
Here is the input file :

Code: Select all

<!DOCTYPE html>
<html><head><meta content="text/html; ">
<!--  http://w3schools.invisionzone.com/forum/6-css/  -->
<title>Jesus'sWordsOnly-3-Rows-RO.html</title>
<style>
body {
     background-color: rgb(113, 30 , 19);
}
table {
border-style: none;  
}
tr {
border-style: none;  
}

.trBetween {

}

.td1 {
color: rgb(0, 0, 0);
vertical-align: top;
width: 50%;
font-size: 17px;
border-style:none;
//border-color: white white rgb(246, 244, 242) white; 
//border-width: 2px;
background-color: rgb(232, 230 , 228);
}

.td2 {
color: rgb(0, 0, 0);
vertical-align: top;
width: 50%;
font-size: 18px;
border-style:none;
//border-color: white white rgb(246, 244, 242) white; 
//border-width: 2px;
background-color: rgb(246, 244, 242); }
</style>
</head>
<body>
<h6 style="text-align: center; color: rgb(246, 244, 242);">Jesus's-Words-Only.html</h6>

<table style="text-align: left; width: 100%;">
<tbody>

<tr>
<td class="td1" contenteditable="false" style="text-align: center;"> 
Hover over Bible references in this column 
<br>to see the GNT version .
</td>
<td class="td2" contenteditable="false" style="text-align: center;"> 
Jesus's words in ESV in this column .
</td>
</tr>

<tr class="trBetween">&nbsp;</tr>
<tr>
<td class="td1" contenteditable="false" style="vertical-align: top;"> 
The Young Jesus at the Temple (Luke 2:41-52) To His parents looking for Him, Jesus said.
</td>
<td class="td2" contenteditable="false" style="vertical-align: top; background-color: rgb(246, 244, 242); "> 
Why were you looking for me? Did you not know that I must be in my Father's house?
</td>
</tr>

<tr class="trBetween">&nbsp;</tr>
<tr>

<td class="td1" contenteditable="false" style="vertical-align: top;"> 
John Baptizes Jesus (Matthew 3:13-17, Mark 1:9-11, Luke 3:21-3, John 1:33-4) John protests.
</td>
<td class="td2" contenteditable="false" style="vertical-align: top; background-color: rgb(246, 244, 242); "> 
Let it be so now, for thus it is fitting for us to fulfill all righteousness.
</td>
</tr>

</tbody>
</table>
<!--StartMessageHere-->
<script src="https://www.biblegateway.com/public/link-to-us/tooltips/bglinks.js" type="text/javascript">
</script>
<script type="text/javascript">
BGLinks.version = "GNT";
BGLinks.linkVerses();
</script>
<div id="bg_popup-container"></div>
</body>
</html>
AND here is program code :

Code: Select all

; ReadAll-CollectAsOneString-Missing(body)(html).pb 
; https://www.purebasic.fr/english/viewforum.php?f=13 
Global OneLine$ , OutputString$ , HtmlLineIn$ 
Global StartingHtmlElement$ = "<td" , EndingHtmlElement$ = "</td>"
Global LEN_StartingHtmlElement = Len(StartingHtmlElement$) , LEN_EndingHtmlElement = Len(EndingHtmlElement$)
Global Starting_HitsCount , Ending_HitsCount , OutputLines$
Global OpenWindow_0 , EditorArea , EditorAreaItemsCOUNT , StartTagStr , EndTagStr , CollectBtn ,
       SaveAsHtmlBtn , CollectWithinBtn , ExitBtn , HelpBtn , OpenInputBtn , 
       OpenThisFilename$  , EndOfProgram$ = "no" , InputFileIsOpen = 0 ,Result = 0 , 
       OutputFileName$ , OutputFileIsOpen = 0 , CurrentDIR$ = GetCurrentDirectory() , 
       HtmlHeader$  = "<!DOCTYPE html>" + Chr(10) + "<html>" + Chr(10) + "<head>" + Chr(10) +    
                      "<meta charset=" + Chr(34) + "UTF-8" + Chr(34) + ">" + Chr(10) +  
                      "<meta name=" + Chr(34) + "viewport"  + Chr(34) + "content="  + Chr(34) + "width=device-width, initial-scale=1.0" + Chr(34) + ">" + Chr(10) + Chr(13) + 
                      "<title></title>" + Chr(10) + "</head>" + Chr(10) +  
                      "<body>" ,
       HtmlTrailer$ = "</body></html>" , LengthOutputString = 0 , GtPosition = 0 , MidStringResult$ , 
       CollectWithinElement$ = "NO"  
;
Declare OpenInputFile()
Declare SaveFileAs()
Declare SplitOutputStringAT()
Declare WriteEditorAreaToFile()
;
Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 575)
  Window_0 = OpenWindow(#PB_Any, x, y, 600, 575, "Collect Html by Element", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget| #PB_Window_TitleBar  | #PB_Window_SizeGadget | #PB_Window_ScreenCentered )
  
  FirstContainer = ContainerGadget(#PB_Any, 10, 6, 580, 64, #PB_Container_Single )
  StartTagStr = StringGadget(#PB_Any, 16, 6, 60, 25, "<td")
  GadgetToolTip(StartTagStr, "Enter START TAG  <p  gets better results than  <p>")
  SetGadgetColor(StartTagStr, #PB_Gadget_FrontColor,RGB(64,0,64))
  EndTagStr = StringGadget(#PB_Any, 89, 6, 60, 25, "</td>")
  GadgetToolTip(EndTagStr, "Enter Full End Tag , like this </p> .")
  SetGadgetColor(EndTagStr, #PB_Gadget_FrontColor,RGB(0,0,0))
  SetGadgetColor(EndTagStr, #PB_Gadget_BackColor,RGB(255,255,255))
  CollectBtn = ButtonGadget(#PB_Any, 180, 6, 120, 25, "Collect Elements")
  GadgetToolTip(CollectBtn, "Collect Elements , show in EditArea Below .")
  CollectWithinBtn = ButtonGadget(#PB_Any, 180, 34, 150, 25, "Collect Within Elements")
  GadgetToolTip(CollectWithinBtn, "Collect Stuff WITHIN the Element Tag , show in EditArea Below .")
  SaveAsHtmlBtn = ButtonGadget(#PB_Any, 320, 6, 86, 25, "Save As Html")
  GadgetToolTip(SaveAsHtmlBtn, "Looking Good , Save It NOW !")
  ExitBtn = ButtonGadget(#PB_Any, 436, 6, 80, 25, "Exit") 
  GadgetToolTip(ExitBtn, "Exit This Program , Bye , Bye !  :)  .")
  HelpBtn = ButtonGadget(#PB_Any, 526, 6, 40, 25, "?")
  GadgetToolTip(HelpBtn, "Show Program HELP File In Browsr.")
  CloseGadgetList()
  
  EditorArea = EditorGadget(#PB_Any, 10, 80, 580, 475, #ES_MULTILINE|#ES_AUTOVSCROLL|#WS_VSCROLL|#WS_HSCROLL ) 
  GadgetToolTip(EditorArea, "This Is Where We Build The Html Page")
 EndProcedure ; OpenWindow_0  
;
 Procedure OpenInputFile() ; |All files (*.*)|*.*
   MessageRequester("  " , " OpenInputFile() " )
;  MessageRequester("" , "CurrentDIR$ = " + CurrentDIR$ ) 
   ClearGadgetItems(EditArea)   
   OpenThisFilename$ = "Jesus'sWordsOnly-3-Rows-RO.html"
   Goto SkipOpenFileRequester
   InputFileIsOpen = InputFileIsOpen + 1 
   If InputFileIsOpen > 1 : CloseFile(2) : InputFileIsOpen = 0
   EndIf 
  StandardFile$ = GetCurrentDirectory()   ; set initial file+path to display
  Pattern$ = "Html (*.html)|*.html;*.html|Text (*.txt)|*.txt"
  Pattern = 0    ; use the first of the three possible patterns as standard
  OpenThisFilename$ = OpenFileRequester("Please choose file to load", StandardFile$, Pattern$, Pattern)
  If OpenThisFilename$
    MessageRequester("Information", "You have selected following file:" + Chr(10) + OpenThisFilename$, 0)
  Else
    MessageRequester("Information", "The requester was canceled.", 0) 
  EndIf
SkipOpenFileRequester:  
ReadFile(2, OpenThisFilename$) 
Repeat
  
OneLine$ = ReadString(2 ) ;=====================================================================

If OneLine$ = "<!--StartMessageHere-->" : ShowMessageRequester$ = "yes" : EndIf 
If ShowMessageRequester$ = "yes" 
  MessageRequester(" OneLine$ " , " OneLine$ " + OneLine$ ) 
EndIf 


OutputString$ = OutputString$ + OneLine$ 
Until Eof(2)
  CloseFile(2)
;
EndProcedure ; OpenInputFile()
; 
Procedure SplitOutputStringAT() ; OutputString$ 

  LengthOutputString = Len(OutputString$)
  GtPosition = 1
  LastGtPosition = 1
Repeat
  GtPosition = FindString(OutputString$, ">" , GtPosition )
  GtPosition = GtPosition + 1
  MidStringResult$ = Mid(OutputString$, LastGtPosition , GtPosition - LastGtPosition )
  LastGtPosition = GtPosition + 1
  AddGadgetItem(EditorArea ,-1 , MidStringResult$)
  OutputString$ = InsertString(OutputString$ , Chr(13) , GtPosition)
Until GtPosition + 2 > LengthOutputString 
; NO File to Close 
EndProcedure ; SplitOutputStringAT()
;
Procedure WriteEditorAreaToFile() 
    EditorAreaItemsCOUNT = CountGadgetItems(EditorArea)
    If CreateFile(1, "HtmlLinesSplitAtGT.html") ; If file can be created... 
      For k = 0 To EditorAreaItemsCOUNT - 1
        WriteStringN(1, GetGadgetItemText(EditorArea, k ) ) 
        Next
;        WriteStringN(1, HtmlTrailer$ ) 
    CloseFile(1) 
  Else   
    MessageRequester("OpenFile(1,  ", "Can't Open File = " + OutputFileName$ )    
  EndIf 
EXITWriteEditorAreaToFile: 
EndProcedure ; WriteEditorAreaToFile()
;
ClearGadgetItems(EditorArea)
OpenWindow_0()
OpenInputFile()
SplitOutputStringAT()
WriteEditorAreaToFile()
;
;
Repeat
  Select WaitWindowEvent() 
    Case #PB_Event_Gadget 

    Case #PB_Event_CloseWindow : End 
  EndSelect 
Until event = #PB_Event_CloseWindow 
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: I need another set of eyes , mine not working so good .

Post by infratec »

Very bad that your code is not compilable :(
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: I need another set of eyes , mine not working so good .

Post by infratec »

Code: Select all

Procedure SplitOutputStringAT() ; OutputString$
  
  Protected GtPosition.i, LastGtPosition.i, MidStringResult$
  
  
  GtPosition = 1
  LastGtPosition = 1
  Repeat
    GtPosition = FindString(OutputString$, ">" , GtPosition )
    If GtPosition
      GtPosition = GtPosition + 1
      MidStringResult$ = Mid(OutputString$, LastGtPosition , GtPosition - LastGtPosition)
      MidStringResult$ = Trim(MidStringResult$, #CR$)
      MidStringResult$ = Trim(MidStringResult$, #LF$)
      LastGtPosition = GtPosition
      AddGadgetItem(EditorArea, -1, MidStringResult$)
      OutputString$ = InsertString(OutputString$, #CR$, GtPosition)
    EndIf
  Until GtPosition = 0
  ; NO File to Close
EndProcedure ; SplitOutputStringAT()
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: I need another set of eyes , mine not working so good .

Post by vmars316 »

Thank you very much !
I have never used the Trim or Protected before .

From the Html.help :
Protected allows a variable to be accessed only in a Procedure even if the same variable has been declared as Global in the main program. Protected in its function is often known as 'Local' from other BASIC dialects. Each variable can have a default value directly assigned to it. If a type is specified after Protected, the default type is changed for this declaration. Protected can also be used with arrays, lists and maps.

The value of the local variable will be reinitialized at each procedure call. To avoid this, you can use the keyword Static, to separate global from local variables while keeping their values.
Example: With variable

Global a
a = 10

Procedure Change()
Protected a
a = 20
EndProcedure

Change()
Debug a ; Will print 10, as the variable has been protected.

"Protected allows a variable to be accessed only in a Procedure "
But the example seems to show that it can't be changed within a procedure .
Does the declared as a Global change that behavior ?
Thanks
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: I need another set of eyes , mine not working so good .

Post by Demivec »

vmars316 wrote: Example: With variable

Global a
a = 10

Procedure Change()
Protected a
a = 20
EndProcedure

Change()
Debug a ; Will print 10, as the variable has been protected.

"Protected allows a variable to be accessed only in a Procedure "
But the example seems to show that it can't be changed within a procedure .
Add 'Debug a' in the procedure to see the value in the procedure. The whole point is that there are two separate variables with separate values even though they have the same name if 'Protected' is used. It also shows that a Global variable's scope can be excluded from a procedure if desired
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: I need another set of eyes , mine not working so good .

Post by infratec »

To avoid trouble use

Code: Select all

EnableExplicit
At beginning of the code.

General I always use Protected in a Procedure when the variable is only used inside of the Procedure.
This avoids also very long searching for a bug if you have unfortunately 2 variables with the same name.

I also don't use Global if I can avoid it.
And in most times you can avoid it :wink:
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: I need another set of eyes , mine not working so good .

Post by vmars316 »

Demivec wrote:
vmars316 wrote: Example: With variable

Global a
a = 10

Procedure Change()
Protected a
a = 20
EndProcedure

Change()
Debug a ; Will print 10, as the variable has been protected.

"Protected allows a variable to be accessed only in a Procedure "
But the example seems to show that it can't be changed within a procedure .
Add 'Debug a' in the procedure to see the value in the procedure. The whole point is that there are two separate variables with separate values even though they have the same name if 'Protected' is used. It also shows that a Global variable's scope can be excluded from a procedure if desired
Ok , I see . Thanks
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: I need another set of eyes , mine not working so good .

Post by vmars316 »

infratec wrote:To avoid trouble use

Code: Select all

EnableExplicit
At beginning of the code.

General I always use Protected in a Procedure when the variable is only used inside of the Procedure.
This avoids also very long searching for a bug if you have unfortunately 2 variables with the same name.

I also don't use Global if I can avoid it.
And in most times you can avoid it :wink:
Thanks infratec .
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
Kiffi
Addict
Addict
Posts: 1362
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: I need another set of eyes , mine not working so good .

Post by Kiffi »

vmars316 wrote:
infratec wrote:To avoid trouble use

Code: Select all

EnableExplicit
Thanks infratec .
I'm having a déjà vu.
Hygge
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: I need another set of eyes , mine not working so good .

Post by skywalk »

EnableExplicit should be ON by default! :evil:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Caronte3D
Addict
Addict
Posts: 1056
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: I need another set of eyes , mine not working so good .

Post by Caronte3D »

+1000
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: I need another set of eyes , mine not working so good .

Post by vmars316 »

Now I see more:

Code: Select all

;Example: With variable

Global a
a = 10

Procedure Change()
Protected a
a = 20
Debug a 
EndProcedure
;
Procedure ChangeAgain()
Protected a
a = 30
Debug a 
EndProcedure

Change()
ChangeAgain() 
Debug a ; Will print 10, as the variable has been protected.

;"Protected allows a variable to be accessed only in a Procedure "
;But the example seems To show that it can't be changed within a procedure .
20
30
10

Thanks
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: I need another set of eyes , mine not working so good .

Post by infratec »

To avoid Global, which is dangerous, use a structure with all your 'global' stuff:

Code: Select all

EnableExplicit  ; you still didn't use it ;)


Structure MyEverywhereNeededVariablesStructure
  a.i
EndStructure


Global a.i


Procedure Change()
  Protected a.i
  a = 20
  Debug a
EndProcedure
;
Procedure ChangeAgain()
  Protected a.i
  a = 30
  Debug a
EndProcedure


Procedure ChangeMyEverywhereNeededVariables(*Variables.MyEverywhereNeededVariablesStructure)
  Protected a.i
  a = 40
  Debug a
  *Variables\a = 20
EndProcedure


Define MyEverywhereNeededVariables.MyEverywhereNeededVariablesStructure

MyEverywhereNeededVariables\a = 10

a = 10
Change()
ChangeAgain()
Debug a
Debug "---"
Debug MyEverywhereNeededVariables\a
ChangeMyEverywhereNeededVariables(@MyEverywhereNeededVariables)
Debug MyEverywhereNeededVariables\a
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: I need another set of eyes , mine not working so good .

Post by vmars316 »

Ah..
Very cool .
Thanks !
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: I need another set of eyes , mine not working so good .

Post by Saki »

Hi vmars316

Globals are a mighty feature !

But it's very dangerous.
Coding without EnableExplicit is a stupid thing to do, sorry, no offense, but it's a fact !

You can also make it so, that you have global variable
e.g. always writes like this or similar marked :

Code: Select all

My_Variable = 100 ; Local

My_Variable__ = 100 ; Global

My_Variable$ = "100" ; Local

My_Variable__$ = "100" ; Global
So you can simple ever found all globals.

But you should to do what you're told.
If you don't do that you will always fail and people will not help you anymore, because you ignore their friendly advice.

Best regards Saki
地球上の平和
Post Reply