Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:07 24 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : double quote in strings: question

Author Message
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4222
Posted: 10:13am 04 Oct 2024
Copy link to clipboard 
Print this post

For most of you this will be simple, but I have not found a solution yet.

In a basic program you can

a$="hello"


But if you want to execute this from a script file you have to

EXECUTE " a$="hello" "


But how to get the double quotes around the "hello". I tried ""hello"" and \"hello\"
and chr$(34)hellochr$(34)...

Just have not found it yet...

anyone ?

Volhout
PicomiteVGA PETSCII ROBOTS
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1113
Posted: 10:18am 04 Oct 2024
Copy link to clipboard 
Print this post

? CHR$(34);"HELLO";CHR$(34)


or
a$=CHR$(34)+"HELLO"+CHR$(34)


with EXECUTE I get an Error: Unknown command
Edited 2024-10-04 20:25 by Martin H.
'no comment
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9105
Posted: 10:23am 04 Oct 2024
Copy link to clipboard 
Print this post

a$=chr$(34)+hello+chr$(34)

or

PicoMite Manual page 27

OPTION ESCAPE
a$="\qhello\q"
a$="\031hello\031"
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4036
Posted: 10:44am 04 Oct 2024
Copy link to clipboard 
Print this post

  matherp said  
OPTION ESCAPE
a$="\qhello\q"
a$="\031hello\031"


Presumably you mean:

a$="\034hello\034"


But IMO that's a snafu as (by C convention) that should be the syntax for an octal sequence, not a decimal sequence.

If you aren't on the PicoMite then these might be of use:

' Decodes a string containing "C" escape codes.
'
' Note: does not handle octal escape codes.
'
' state%:
'   0 = base state
'   1 = looking for escape code
'   2 = expecting 1st hex digit
'   3 = expecting 2nd hex digit
Function str.decode$(s$)
 Local ad%, ch%, prev%, state%, t$

 For ad% = Peek(VarAddr s$) + 1 TO Peek(VarAddr s$) + Len(s$)
   ch% = Peek(Byte ad%)
   Select Case ch%
     Case &h5C ' \
       state% = 1
     Case Else
       Select Case state%
         Case 0
           Cat t$, Chr$(ch%)
         Case 1
           Select Case ch%
             Case &h22 : ch% = &h22 ' \"
             Case &h27 : ch% = &h27 ' \'
             Case &h30 : ch% = &h00 ' \0
             Case &h3F : ch% = &h3F ' \?
             Case &h5C : ch% = &h5C ' \
             Case &h61 : ch% = &h07 ' \a
             Case &h62 : ch% = &h08 ' \b
             Case &h65 : ch% = &h1B ' \e
             Case &h66 : ch% = &h0C ' \f
             Case &h6E : ch% = &h0A ' \n
             Case &h71 : ch% = &h22 ' \q
             Case &h72 : ch% = &h0D ' \r
             Case &h74 : ch% = &h09 ' \t
             Case &h76 : ch% = &h0B ' \v
             Case &h78 : state% = 2 ' \x
             Case Else : Cat t$, "\"
           End Select
           If state% = 1 Then
             Cat t$, Chr$(ch%)
             state% = 0
           EndIf
         Case 2
           Select Case ch%
             Case &h30 To &h39, &h41 To &h46, &h61 To &h66 ' 0-9, A-F, a-f
               prev% = ch%
               state% = 3
             Case Else
               Cat t$, "\x" + Chr$(ch%)
               state% = 0
           End Select
         Case 3
           Select Case ch%
             Case &h30 To &h39, &h41 To &h46, &h61 To &h66 ' 0-9, A-F, a-f
               Cat t$, Chr$(Val("&h" + Chr$(prev%) + Chr$(ch%)))
             Case Else
               Cat t$, "\x" + Chr$(prev%) + Chr$(ch%)
           End Select
           state% = 0
       End Select
   End Select
 Next

 Select Case state%
   Case 1 : Cat t$, "\"
   Case 2 : Cat t$, "\x"
   Case 3 : Cat t$, "\x" + Chr$(prev%)
 End Select

 str.decode$ = t$
End Function

' Encodes a string using "C" escape codes.
Function str.encode$(s$)
 Local ad%, ch%, t$
 For ad% = Peek(VarAddr s$) + 1 TO Peek(VarAddr s$) + Len(s$)
   ch% = Peek(Byte ad%)
   Select Case ch%
     Case &h00 : Cat t$, "\0"
     Case &h07 : Cat t$, "\a"
     Case &h08 : Cat t$, "\b"
     Case &h09 : Cat t$, "\t"
     Case &h0A : Cat t$, "\n"
     Case &h0B : Cat t$, "\v"
     Case &h0C : Cat t$, "\f"
     Case &h0D : Cat t$, "\r"
     Case &h1B : Cat t$, "\e"
     Case &h22 : Cat t$, "\q"
     Case &h27 : Cat t$, "\'"
     Case &h3F : Cat t$, "\?"
     Case &h5C : Cat t$, "\\"
     Case < &h20, >= &h7F : Cat t$, "\x" + Hex$(ch%, 2)
     Case Else : Cat t$, Chr$(ch%)
   End Select
 Next
 str.encode$ = t$
End Function


allowing you to write either:

a$=str.decode$("\qhello\q")


or:

a$=str.decode$("\x22hello\x22")


Best wishes,

Tom
Edited 2024-10-04 20:52 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9105
Posted: 11:01am 04 Oct 2024
Copy link to clipboard 
Print this post

Nobody uses octal anymore so I re-purposed that syntax - yes it should be \034

From the manual
  Quote  \nnn any The byte whose value is given by nnn interpreted as a decimal number
\&hh any The byte whose value is given by hh… interpreted as a hexadecimal number
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4036
Posted: 11:07am 04 Oct 2024
Copy link to clipboard 
Print this post

  matherp said  Nobody uses octal anymore so I re-purposed that syntax - yes it should be \034


Whilst I personally stay clear of octal I still think it dangerous to subvert convention in an engineering context such as a programming language.

YMMV,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4222
Posted: 11:11am 04 Oct 2024
Copy link to clipboard 
Print this post

Thank you for your help.

Volhout
PicomiteVGA PETSCII ROBOTS
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4036
Posted: 11:25am 04 Oct 2024
Copy link to clipboard 
Print this post

  thwill said  ... I still think it dangerous to subvert convention in an engineering context such as a programming language.


... though I now observe you are not the only one to have done it : https://freebasic.net/wiki/TblEscapeSequences

I'll get back in my box,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 896
Posted: 11:29am 04 Oct 2024
Copy link to clipboard 
Print this post

The EXECUTE command expects a string.
Use chr$(34) to put the extra quotes.(or OPTION ESCAPE with \q )
pass to EXECUTE as below.

execute "a$="+chr$(34)+"hello world"+chr$(34)


> ? a$
hello world
Latest F4 Latest H7
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4222
Posted: 11:38am 04 Oct 2024
Copy link to clipboard 
Print this post

Thank you for your help.

I looked at your proposals and I feel so stupid.

I wanto to add test to a log (in this case a text array called r$() and use pointer i. This routine will later change into writing to a file in the filesystem.

sub add_log(a$)
 r$(i)=a$
 inc i,1
end sub


I run a script file, and when it it should store text in the log file this is the current code (I want to execute txt$).


   read command$
   select case command$
     case "EXE"
       read p:read txt$
       execute txt$+chr$(13)


The txt$ is read from DATA statements

 data "EXE",0,"add_log(str$(a)+chr$(32)+chr$(109)+chr$(87))"


and above works. It adds the value of a (i.e. 100), and then the text " mW".
i.e. "100 mW" to r$(i)

I try to use the escape codes, so I won't have to translate every ASCII character manually. So I try...

 data "EXE",0,"add_log(str$(a)+\q mW\q)"
 data "EXE",0,"add_log(str$(a)+"\q mW\q")"
 data "EXE",0,"add_log(str$(a)\q mW\q)"
 data "EXE",0,"add_log(str$(a)"\q mW\q")"


O coarse I also tried \034.

B.T.W. MMB4L does error on OPTION ESCAPE

Volhout
Edited 2024-10-04 21:40 by Volhout
PicomiteVGA PETSCII ROBOTS
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4036
Posted: 11:49am 04 Oct 2024
Copy link to clipboard 
Print this post

  Volhout said  B.T.W. MMB4L does error on OPTION ESCAPE


Yes, sorry @Volhout, I don't think I'll ever implement OPTION ESCAPE* in MMB4L, though I have been considering:

a$ = c"String using C escape sequences"
a$ = !"String using FreeBASIC escape sequences"


or something similar.

In the meantime str.encode$() and str.decode$() are your friend .

* At the risk of controversy I think it's a broken design. One issue (in addition to being different from C escapes) is that because it's not on a per-string basis Peter had to "hack" around any place where a string was being used as a file path (which frequently contain the \ character but don't mean it to introduce an escape).

YMMV,

Tom
Edited 2024-10-04 21:50 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6778
Posted: 12:56pm 04 Oct 2024
Copy link to clipboard 
Print this post

I've seen BASIC implementations that allow both single and double quotes. You have to match the pairs though:

print "'this is a string'"
'this is a string'

print '"this is another"'
"this is another"

print "'a load of rubbish"'
'a load of rubbish
(and probably an error message)

Neat, but probably non-standard.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3800
Posted: 01:53pm 04 Oct 2024
Copy link to clipboard 
Print this post

Mick,

... and then can't have comments starting with a single quote (I guess).

John
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3800
Posted: 01:55pm 04 Oct 2024
Copy link to clipboard 
Print this post

  Volhout said  

   read command$
   select case command$
     case "EXE"
       read p:read txt$
       execute txt$+chr$(13)


The txt$ is read from DATA statements

I suspect the chr$(13) is not needed.

John
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6778
Posted: 03:41pm 04 Oct 2024
Copy link to clipboard 
Print this post

Good point, John. I can't honestly remember.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 339
Posted: 07:47pm 04 Oct 2024
Copy link to clipboard 
Print this post

  disco4now said  The EXECUTE command expects a string.
Use chr$(34) to put the extra quotes.(or OPTION ESCAPE with \q )
pass to EXECUTE as below.

execute "a$="+chr$(34)+"hello world"+chr$(34)


> ? a$
hello world


In some BASICs (such as ANSI) you can do this:

a$="She said, ""I see the tower!"""

using two quotation marks to mean one.

In some you can use either the apostrophe or quotation mark like this:

a$='She said, "I can see the tower!"'
b$="This is Sam's coat"

whichever mark is needed inside, the opposite one is used to enclose the entire string.
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024