简体   繁体   English

Free Basic中的输入数组

[英]Input array in Free Basic

Now, I'm programming by Free Basic and I'm looking for a way to get values of an array in a single line . 现在,我正在使用Free Basic进行编程,并且正在寻找一种方法来在一行中获取数组的值。

For example, if I want to get 2 integers of an array in a single line, I write this: 例如,如果我想在一行中获得一个数组的2个整数,则可以这样写:

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 . 但是该程序会多行获取Array值。 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? 有人可以帮我吗?

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. 但是为此,您需要使用line input而不是input ,因为input只会在第一个逗号之前返回字符串,而line input将返回整个字符串。

Unfortunately, FreeBasic's weakness is string parsing, so you need to use a library or build your own functions to parse out the numbers. 不幸的是,FreeBasic的弱点是字符串解析,因此您需要使用一个库或构建自己的函数来解析数字。 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. 确保在声明数组时使用redim ,以便可以在运行时调整数组的大小。

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. 如果您需要读取大多数CSV文件,则相同的技术非常有效。

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 换句话说,如果您: input #1, string1,x

and the file contains 并且文件包含

"hello"world", 2

you will only get hello and 2 back. 您只会打招呼和2。 (As of FB v 1.01) I consider this a bug, since you can have a string with embedded quotes elsewhere. (从FB v 1.01开始),我认为这是一个错误,因为您可以在其他地方使用带嵌入式引号的字符串。

BTW, writing a CSV file is easy using: 顺便说一句,编写CSV文件很容易:

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

Hope this helps, I've seen the same question in a few other places. 希望这会有所帮助,我在其他一些地方也看到了相同的问题。

您必须输入“字符串”,然后将字符串拆分为给定值的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM