locked
How do I pull a number from a document? RRS feed

  • Question

  • Alright, I am making an index of sorts and am using a .txt document to store the information the program pulls from.(Using small basic)

    What I am trying to do is pull a number from the document using the File.Readline() command. in it I put the to items, the exact location of the file,(a pain in the butt to do/ very finicky.) and after a comma put the line number(5,3, etc.) 

    I am assigning the vallue i get, (it ranges from 0 to 300,000) , to an array[]. 

    I want to add several of the values together using a for statement:

    For i = 1 to V  {a pre assigned value}

       total = total + Array[i]

    endfor

    The problem is that it is not adding the values. Just stringing them together.

    So adding the array values 400, 300, and five thousand ends up like this: Total = 400  300  5,000

    How do I get the values to add together?

    There are two way's I think I could do it. 

    Either I have to enter the individual values in a Textwindow.ReadNumber() which would take forever.

    It also might work for me to store the numbers on individual lines, so I don't have to cut the value's from the text using the text.getsubtext tool, but that would also be hard to do.

    Friday, April 15, 2016 7:14 PM

Answers

  • In Small Basic when you use '+' to add 2 variables it will concatenate if one of them is not a number and arithmetically add them if they are both numbers.

    So somehow one of them is not a number if they are being concatonated (string appending).

    Try looking atthe values you are tyring to add (TextWindow.Writeline) and see the issue, otherwise post a small sample wecan run to help (i.e. not using files we don't have).

    Examle below showing the ffect.

    var1 = "100"
    var2 = "-300.5"
    TextWindow.WriteLine(var1+var2) 'Arithmetic addition
    
    var1 = "100"
    var2 = "-300.5,"
    TextWindow.WriteLine(var1+var2) 'String concatenation

    Friday, April 15, 2016 7:22 PM

All replies

  • In Small Basic when you use '+' to add 2 variables it will concatenate if one of them is not a number and arithmetically add them if they are both numbers.

    So somehow one of them is not a number if they are being concatonated (string appending).

    Try looking atthe values you are tyring to add (TextWindow.Writeline) and see the issue, otherwise post a small sample wecan run to help (i.e. not using files we don't have).

    Examle below showing the ffect.

    var1 = "100"
    var2 = "-300.5"
    TextWindow.WriteLine(var1+var2) 'Arithmetic addition
    
    var1 = "100"
    var2 = "-300.5,"
    TextWindow.WriteLine(var1+var2) 'String concatenation

    Friday, April 15, 2016 7:22 PM
  • Alright the two main issues then are this,

    first, I have puncuated the numbers like the american I am. so,(300000 = 300,000)

    that might be the problem, the other thing is that the way I stored the answers with spaces after them like this.

    300____. so I will work on a better way to retrieve the information so that it does not have the spaces, Does it matter if I have commas separating thousand from hundreds?

    Friday, April 15, 2016 8:54 PM
  • Spaces are ok, but the commas will be the problem most likely.  Internally SB uses . for decimal - there are ways round this using extensions for different locales, but best use . for portability etc.

    Don't usec ommas (,) anywhere if you want things read as numbers.

    If it is a CSV from excel or other format you cannot change, then extensions may help but we would need to see the exact format to suggest a solution using SB extension.

    Friday, April 15, 2016 9:05 PM
  • Nevermind about the second question, i got rid of the commas.

    Thankyou for your answer so quickly.

    Friday, April 15, 2016 9:15 PM