-- print the first non-empty line
repeat
line = io.read()
until line ~= ""
print(line)
So, the logic is like, keep reading the characters until the next character is empty? Thanks.
Did you read the comment?
-- print the first non-empty line
It prints the first non-empty line.
This is achieved by reading the input (keyboard, unless redirected) until a non-empty line has been entered. This line is then printed.
You read a line, check wether it is not an empty string "". If it is not, you read the next line. If it is you won't read again but print what you've read last.
I know how to print a variable and a text but I want to make it in the same line, If i Use:
print "text" MYVARIABLE
I got an exception
Expected command
So the question is: there is a way to print text and variable on the same line?
Thank you
Yes! The word primitive is what you are after- see the dictionary definition here.
In brief, you can follow the syntax:
print word "Count turtles: " count turtles
Or
print ( word "There are " count turtles " turtles right now" )
i'm completely new to Lua and i have got this error multiple times "'end' expected (to close 'while' at line 3) near ''" i have checked to find answers online and no luck so i was hoping someone could help me fix this problem, thanks alot
Here is my code :
print ("Welcome to the maze")
while input ~= "leave" do
print ("What do you want to do first? Leave or inspect?")
input = io.read()
if input == "inspect" then
print (" You venture towards the maze.")
end
if input == "leave" then
print ("You turn around and run.")
end
I've never seen lua, but i think reading the error will be the solution:
'end' expected (to close 'while' at line 3)
So i need to put end to code:
print ("Welcome to the maze")
while input ~= "leave" do
print ("What do you want to do first? Leave or inspect?")
input = io.read()
if input == "inspect" then
print (" You venture towards the maze.")
end
if input == "leave" then
print ("You turn around and run.")
end
end
Here's an example in python3 of what I want to do in Fortran:
str1 = "Hello"
str2 = " World!"
print(str1 + str2)
# And then the result would be "Hello World!"
When I do:
print "(A)", str1, str2
It puts it on a separate line. If anyone knows how to help please answer.
The literal answer to string concatenation, using the // operator, is given in another answer. Note, particularly, that you likely want to TRIM the first argument.
But there is another interesting concept your question raises, and that is format reversion.
With the format (A) we have one format item. In the output list str1, str2 we have two output items. In a general output statement we apply each format item (with repeat counts) to a corresponding output item. So, str1 is processed with the first format item A, and a string appears.
Come the second output item str2 we've already used the single format item, reaching the end of the format item list. The result is that we see this format reversion: that is, we go back to the first item in the list. After, crucially, we start a new line.
So, if we just want to print those two items to one line (with no space or blank line between them) we could use (neglecting trimming for clarity)
print "(A)", str1//str2
or we could use a format which hasn't this reversion
print "(2A)", str1, str2
print "(A, A)", str1, str2
The first concatenates the two character variables to give one, longer, which is then printed as a single output item. The second prints both individually.
Coming to your particular example
character(12), parameter :: str1="Hello" ! Intentionally longer - trailing blanks
character(12), parameter :: str2=" World!"
print "(2A)", TRIM(str1), TRIM(str2)
end
will have output like
Hello World!
with that middle space because TRIM won't remove the leading space from str2. More widely, though we won't have the leading space there for us, and we want to add it in the output.
Naturally, concatenation still works (I'm back to assuming no-trimming)
character(*), parameter :: str1="Hello" ! No trailing blank
character(*), parameter :: str2="World!"
print "(A)", str1//" "//str2
end
but we can choose our format, using the X edit descriptor, to add a space
print "(2(A,1X))", str1, str2
print "(A,1X,A)", str1, str2
print "(2(A,:,1X))", str1, str2
where this final one has the useful colon edit descriptor (outside scope of this answer).
Probably close to what you want:
Concatenate two strings in Fortran
zz = trim(xx) // trim(yy)
More info
Bing
It looks like this is covered but another useful feature, if you want to print a lot of data on the same line is the following:
character(len=32),dimension(100) :: str
do i=1,100
write(*,fmt="(A)", advance='no') str(i)
end do
write(*,*) ! to go to the next line when you are done
This will print 100 characters on the same line because of advance='no'
You can use another variable to put those into it, and write those two strings in that new variable:
Program combineString
character(len=100) :: text1, text2, text
text1 = "Hello,"
text2 = "My name is X"
write(text,'(A6, X, A20)') text1, text2
write(*,*) text
End Program
And output is:
Hello, My name is X
I have a shell script that has an output like this:
1
Space Cruise (Title)
Ben Prunty Music
FTL
46.4
Now I want to map the lines to an array like this:
mymplayer = { track="", title="", artist="", album="", time="" }
So I'm using io.popen() like this (for testing purposes):
function get_data()
local fh = io.popen("bin/mplayerout.sh")
for l in fh:lines() do print(l) end
end
The problem is that this has the following output:
Space Cruise (Title)
Ben Prunty Music
FTL
46.4
Now, if I make my script's output start with a newline, the output is like this:
<empty line>
Space Cruise (Title)
Ben Prunty Music
FTL
46.4
What am I doing wrong?
Alright, I found the issue. When doing the same in Python, I got the first line - but as 3281 null bytes ('\x00') followed by '\x02\n'.
Looks like the problem is in the script...
It could be that the first line of output from mplayerout.sh ends in a carriage return ("\r") while the others end in newlines ("\n" or "\r\n".)
Then the initial "1" would still be in the output, but the "\r" will cause the terminal to overwrite it with the following characters.
You can see this by using print(string.format("%q", l)). If there is a "\r" embedded in the first line, the output will be:
"1\rSpace Cruise (Title)"
"Ben Prunty Music"
"FTL"
"46.4"