0

Now, I'm programming by Free Basic and I'm looking for a way to get values of an array in a single line.

For example, if I want to get 2 integers of an array in a single line, I write this:

Dim a(2) as integer
Input a(1),a(2)

But my program should get Array length from user.

This is my program:

dim length as integer
input "Enter Array length: ",length
dim a(length) as integer
dim i as integer
for i=1 to length
input a(i)
next
'OTHER CODES...

But this program gets Array values in multi lines. The problem is exactly here. I want to take it in a single line but I don't know that "What I should to do?"

Can anyone help me, please?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Mostafa Farzán
  • 899
  • 2
  • 11
  • 30

3 Answers3

1

Yep. The best way is probably to grab an entire string and parse out the numbers yourself. But for this, you need to use line input instead of input, because input will only return the string before the first comma, where line input returns the entire string.

Unfortunately, FreeBasic's weakness is string parsing, so you need to use a library or build your own functions to parse out the numbers. Here's an example:

declare sub explode_to_integers(s as string, a() as integer, delimiter as string=",")

sub explode_to_integers(s as string, a() as integer, delimiter as string = ",")
    dim i as integer
    dim idx as integer = 0
    while len(s)
        if idx > ubound(a) then
            redim preserve a(idx) as integer
        end if
        i = instr(s, delimiter)
        if i then
            a(idx) = cast(integer, left(s, i-1))
            s = right(s, len(s)-i)
        else
            a(idx) = cast(integer, s)
            s = ""
        end if
        idx += 1
    wend
end sub

And you'd use it like so:

dim numbers as string
redim a() as integer

line input "Enter numbers: ", numbers

explode_to_integers(numbers, a()) '// split string by comma and put values into a()

dim i as integer
for i = 0 to ubound(a)
    print a(i)
next i
end

Make sure, when you declare your array, that you use redim, so the array can be resized during run time.

Joe
  • 1,330
  • 13
  • 15
1

If your input is all numbers (without commas), and/or text WITHOUT quotes, then it's pretty simple:

Dim as integer x,x1,y,y1    
Dim as string string1,string2

print "Be sure to use commas between values, if you need a comma in a string,"  
print "use double quotes around the string."
Input "Enter x,x1,string1,y,y1,string2", x,x1,string1,y,y1,string2

The same technique works really well if you need to read most CSV files.

Input #filehandle, x,x1,string1,y,y1,string2

Be aware that this will NOT handle embedded quotes in strings, it will truncate the string at the second double quote, NOT at the next unquoted comma.

In other words, if you: input #1, string1,x

and the file contains

"hello"world", 2

you will only get hello and 2 back. (As of FB v 1.01) I consider this a bug, since you can have a string with embedded quotes elsewhere.

BTW, writing a CSV file is easy using:

Write #filehandle, x,x1,string1,y,y2,string2

Hope this helps, I've seen the same question in a few other places.

Jérémie Bertrand
  • 3,025
  • 3
  • 44
  • 53
Paul Tice
  • 11
  • 1
1

you have to input a "string", then split the string in the number of given values.

nabuchodonossor
  • 2,095
  • 20
  • 18