none
strange writeline behavior RRS feed

  • Question

  • TextWindow.WriteLine(5+3+"abc"+5+3) results in 8abc53.  Why is the second 5+3 treated differently than the first?

    Monday, March 5, 2012 12:28 AM

Answers

  • TextWindow.WriteLine(5+3+"abc"+5+3) results in 8abc53.  Why is the second 5+3 treated differently than the first?

    If you add parenthesis at the last 5+3 like this -> TextWindow.WriteLine(5+3+"abc"+(5+3))

    it works as desired :-)

    Problem is, in Small Basic, everything is a string. The parser has to interpret whether a '+' operation is an math addition or a string concatenation!

    You can also use Text.Append(text1,text2) to force a concatenation, even if both texts are numbers only!


    Monday, March 5, 2012 1:59 AM
    Answerer

All replies

  • TextWindow.WriteLine(5+3+"abc"+5+3) results in 8abc53.  Why is the second 5+3 treated differently than the first?

    If you add parenthesis at the last 5+3 like this -> TextWindow.WriteLine(5+3+"abc"+(5+3))

    it works as desired :-)

    Problem is, in Small Basic, everything is a string. The parser has to interpret whether a '+' operation is an math addition or a string concatenation!

    You can also use Text.Append(text1,text2) to force a concatenation, even if both texts are numbers only!


    Monday, March 5, 2012 1:59 AM
    Answerer
  • I've got an explanation why it happened so!

    Following the operation order:

    5+3+"abc"+5+3 = 8+"abc"+5+3 = "8abc"+5+3 = "8abc5"+3 = "8abc53"

    Once 1 of the terms becomes a 'string', all operations are interpreted as both terms being a non-numeric value!

    Using '+' results in a concatenation. In other math operations, strings are considered = 0!!!




    Monday, March 5, 2012 2:08 AM
    Answerer
  • Thanks GoToLoop.

    I sure wish the designers of SB had used & for the string concatenation operator, rather than +, so you could count on + always doing the math operation.  It's really bad form to have a parser decide whether + means concatenate or add.  My students will never understand this distinction, and the whole point of SB was supposed to be to make it easy on the newbie.

    With no Print and no help and no EndofFile() function and no ability to easily read a CSV file and no good command syntax documentation, I've got to evaluate if SB is the best tool to teach my students programming.

    Monday, March 5, 2012 2:29 AM
  • There is a way to guarantee a '+' operation to always be an addition math operation. But 1st, let me give you an example to how Small Basic stores its values:

    a= 10
    b= "-5"
    c= a+b+a
    d= 50/c
    TextWindow.WriteLine(c)
    TextWindow.WriteLine(d)

    As you can see, it doesn't matter weather you attribute a number w/ or w/o quotes; if the "string" only have valid number characters, including '-' and '.', that term is a "numeric" string!!!

    If you change b= "-5" to something like a misplaced minus sign at the right side: b= "5-", its value is not a valid number string anymore!

    so, the '+' becomes a concatenation, since at least one of its terms is not numerically valid. Calculation applies as the following:

    c= 10 + "5-" + 10 = "105-" + 10 = "105-10" -> c= "105-10"

    d= 50 / "105-10" = 50 / 0 -> d= 0 !!!!!

    The "+" operation became concatenations, and the other operations like division "/", the invalid string term is considered = 0, resulting in a division by ZERO!

    But it seems Small Basic knows that any # divided by ZERO = ZERO :-D

    Now a way to avoid concatenations:

    Just multiply the dubious variable by 1, like this: b= b*1

    This way, if b is an invalid numeric string, a multiplication results in a ZERO, if it is a valid one, results in the same number, since 1 is a neuter multiplication element ^_^

    w/ b= "5-" -> b= "5-" * 1 = 0  * 1 =  0

    w/ b= "-5" -> b= "-5" * 1 = -5 * 1 = -5

    Hope this "solution" suits you. Peace out!




    • Edited by GoToLoopEditor Monday, March 5, 2012 4:46 AM more clarifications
    Monday, March 5, 2012 3:52 AM
    Answerer
  • With no Print and no help and no EndofFile() function and no ability to easily read a CSV file

     You can treat "CSV" easily. (Read , write)    Try this.

    PGDT=program.Directory+"\CSVtest.csv" 
     File.DeleteFile(PGDT)   
    '  Write  CSV data
      sep=text.GetCharacter(34)   ' this is the point.
      b=sep+12+sep+","+sep+56+sep+","+sep+78+sep+","
      File.AppendContents(PGDT,b)        
      b=sep+900+sep+","+sep+1001+sep+","+sep+1102+sep+","
      File.AppendContents(PGDT,b)   
    '  Read CSV data
     c=File.ReadContents(PGDT)
     TextWindow.WriteLine(c)

    While e<>0
      NN=NN+1
      d=File.ReadLine(PGDT,NN)
      e=text.GetLength(d)
      TextWindow.WriteLine("LineNo=? "+NN+" : "+d)
      MM=0
      f=d
      While f<>""
        midP=text.GetIndexOf(f,",")
        MM=MM+1
        dumy=Text.GetSubText(f,1,midP-1)
        Data[MM]=Text.GetSubText(dumy,2,text.GetLength(dumy)-2)
        TextWindow.WriteLine(MM+" : "+Data[MM])
        f=text.GetSubTextToEnd(f,midP+1)
      EndWhile
    EndWhile

    Note : Your EXCEL can read this data.

             Also you can read EXCEL CSV data by SB .



    Monday, March 5, 2012 6:28 AM
    Answerer
  • Thanks, GoToLoop.

    I think I understand what you are saying, but I don't like the idea of SB automatically making interpretations about what is numeric and what is not numeric.  If I declare zip_code = "40207", I don't ever want that treated as a numeric value.  I much prefer the old $ prepended to the variable name to signify a string, as opposed to a number, but I guess that's out-of-style in this day of object-oriented programming. 

    And I have a HUGE problem with any programming language assuming that division by 0 = 0.  Mathematically, division by 0 is undefined and any attempt to do that should raise an error, whether the platform is a calculator or a program.

    The more I discover about how SB works, the less I like it.

    Monday, March 5, 2012 9:26 AM
  • Thanks for the info, NaochanON.  I think your definition of "easily" and mine must be quite different.

    Myfile.txt contains: Don,1366 Pine St,Wilmington,DE,22101 Fred,104 Jackson St,St. Louis,MO,45587 To read as a CSV file: Open "c:\Myfile.txt" for input as 1 While not eof(1) Read 1,name$,street$,city$,state$,zip$ Wend Close 1

    Well, it won't let me out of the code block now.....

    I believe that this is understandable to a student just learning Basic.

    Monday, March 5, 2012 9:35 AM
  • If I declare zip_code = "40207", I don't ever want that treated as a numeric value.

    The more I discover about how SB works, the less I like it.

    Like I said at 1st reply up above: if you wanna make sure to always concatenate, use Text.Append(text1, text2). For example:

    zip_code=  40207
    zip_suffix=  "030"
    zip_full= Text.Append  ( zip_code, zip_suffix )
    TextWindow.WriteLine ( zip_full )

    Displays 40207030, instead of 40237

    For the opposite case, if you wanna avert a possible concatenation when using '+', just multiply all dubious variables by 1:

    zip_code= 40207
    zip_suffix= "030"
    zip_code= zip_code * 1
    zip_suffix= zip_suffix * 1
    zip_calc=   zip_code + zip_suffix
    TextWindow.WriteLine ( zip_calc )

    This way, if any variable would contain an invalid number term, it becomes a real '0' value, otherwise, it maintains its original value!

    Small Basic stores all variable's values as string. And tries to use them as numerical values when using math operations as much as possible!

    Have patience!   (^_^)





    • Edited by GoToLoopEditor Tuesday, March 6, 2012 5:46 AM 030 not = "030"
    Monday, March 5, 2012 10:16 AM
    Answerer
  • I used dos-basic long long ago.
    At that time ,  I used to write these code. resembles yours.
      DIM s$(10), h(10), w(10)
      OPEN #1:NAME "D:DATA.CSV"
      FOR i=1 TO 10
          READ #1:s$(i),h(i),w(i)
       NEXT i
      CLOSE #1

    Now,  why I use SB?       The reason is following five.
    1) Dim declaration is unnecessary.
      There is no necessity of declaring array beforehand   and  handling is easy.
    2) File treatment is very easy.
      There is no necessity of writing Open, Close, etc.  (  I also want EOF!)
    3)Handling of picture is easy.
      Shape command is very pleasing.
    4)Extensions for  Small Basic
      Useful functions are prepared by Extension, such as SerialPort, Dialog, Print, Process, and Media....etc
    5)Without distinction of a numerical value and a character
      Although you'll got confused at first, it is very convenient if you get used.

    I usually treat Data-logger data.    Like this.  "D:data.csv"

    CH11," CH11",DC,100mV,Off,+100.000,-100.000,[AV],
    CH12," CH12",DC,100mV,Off,+83.330,-83.330,[AV],
    CH13," CH13",DC,100mV,Off,+83.330,-83.330,[AV],
    CH14," CH14",DC,100mV,Off,+83.330,-83.330,[AV],
    CH15," CH15",DC,100mV,Off,+83.330,-83.330,[AV],
    CH16," CH16",DC,100mV,Off,+83.330,-83.330,[AV],
    CH17," CH17",DC,100mV,Off,+83.330,-83.330,[AV],
    CH18," CH18",DC,100mV,Off,+83.330,-83.330,[AV],
    CH19," CH19",DC,100mV,Off,+83.330,-83.330,[AV],
    CH20," CH20",DC,100mV,Off,+83.330,-83.330,[AV],
    Logic/Pulse,Off,
    Value,
    Number,Date Time,CH1,CH2,CH3,CH4,CH5,CH6,CH7,CH8,CH9,CH10,CH11,CH12,CH13,CH14,CH15,CH16,CH17,CH18,CH19,CH20,Alarm1  -10 ,Alarm11 -20,
    NO.,Time,"AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","AV","A12345678910","A12345678910",
    1,2011/11/3 12:37:22,-0.05,-0.05,-0.06,-0.05,-0.05,-0.07,-0.06,-0.06,-0.05,-0.04,-0.067,-0.067,-0.067,-0.075,-0.042,-0.075,-0.067,-0.058,-0.058,-0.050,LLLLLLLLLL,LLLLLLLLLL,
    2,2011/11/3 12:37:27,-0.05,-0.05,-0.06,-0.05,-0.05,-0.07,-0.06,-0.06,-0.05,-0.04,-0.067,-0.058,-0.067,-0.075,-0.042,-0.075,-0.067,-0.058,-0.058,-0.050,LLLLLLLLLL,LLLLLLLLLL,
    3,2011/11/3 12:37:32,+0.06,+0.11,+0.22,+0.12,+0.92,+9.12,+14.04,+15.62,+8.73,+6.03,+20.941,+20.699,+25.524,+28.057,+15.191,+42.523,+31.532,+24.116,+23.566,+19.816,LLLLLLLLLL,LLLLLLLLLL,
    4,2011/11/3 12:37:37,+18.54,+16.81,+19.24,+15.18,+16.11,+25.76,+24.22,+27.81,+17.37,+17.34,+18.283,+17.616,+19.749,+18.899,+16.899,+21.324,+23.099,+20.366,+17.858,+18.158,LLLLLLLLLL,LLLLLLLLLL,
    5,2011/11/3 12:37:42,+18.87,+16.51,+18.12,+14.83,+16.03,+24.17,+22.62,+28.56,+17.83,+17.90,+19.858,+16.766,+19.466,+18.633,+17.099,+19.874,+22.724,+20.774,+17.899,+18.766,LLLLLLLLLL,LLLLLLLLLL,
    6,2011/11/3 12:37:47,+17.84,+17.40,+18.34,+15.17,+16.40,+24.57,

    How dou you write code to treat these data?

    If SB, only this.    I think it easy. 

    PGDT=program.Directory+"\data.csv" 
     
    While e<>0
       NN=NN+1
       d=File.ReadLine(PGDT,NN)
       e=text.GetLength(d)
       MM=0
       f=d
         While f<>""
          midP=text.GetIndexOf(f,",")
          MM=MM+1
          Data[NN][MM]=Text.GetSubText(f,1,midP-1)
          TextWindow.WriteLine("N= "+NN+" M= "+MM+" : "+Data[NN][MM])
          f=text.GetSubTextToEnd(f,midP+1)
         EndWhile
     EndWhile

    Monday, March 5, 2012 12:59 PM
    Answerer
  • Thanks NaochanON, I appreciate your help.

    I don't disagree that these things (string variable concatenation, reading CSV files, etc.) can be done in SB, but for a language that is supposed to cater to those who are first beginning programming, it ought to be easier than this.  Some pretty basic things are missing here, and as a teacher of middle school students, I've got to consider which version of Basic is most suited to helping my students learn. 

    Microsoft advertises that Small Basic is easy to learn and has only 15 commands (like the original Dartmouth had 14 or 15 commands), but obviously SB has hundreds of things you can do whereas Dartmouth Basic had only the 15 commands and 10 functions, and that was it, no extensions.  A student could learn these few things and be productive (hopefully) immediately.

    I realize it is not the teletype world of 1964 and the world of object-oriented programming has been with us for many years, but I still want to use a version of Basic that is as simple as possible, and I'm not sure SB is it right now.

    Thanks for your help.

    Monday, March 5, 2012 2:08 PM
  • Despite some of SB's shortcomings, I think we are going to use it as our platform to introduce our middle school students to programming, at least as a pilot project for the rest of this term.  It is free, after all, unlike many other Basic's, and that's a major consideration.  And there are obviously smart people on this forum we can use for help.  I briefly considered Python, mostly due to the excellent book by Sande, but I've never used Python before and I'd rather stay with a language I've used for awhile (in my case, since around 1970!).

    Microsoft, if you are listening, how about offering the developer's guide book for SB to teachers at a significantly reduced price from the normal $59, like maybe $20?  I think that would help a lot.  I'm a volunteer teacher and that's just way out of my price range.

    We plan to use the PowerPoint slides as our basic curriculum and see how that goes.

    Tuesday, March 6, 2012 4:18 AM