I am working on a project, in which you type your input sentence, and I need to be able to use " and ' in the sentence, such as Input = "I said, "Hi what's up?" print(Input) in which I get an error. If anyone knows how to fix this that would be great.
See https://www.lua.org/pil/2.4.html. Lua has very interesting feature to declare string with square brackets:
input = [[I said, "Hi what's up?"]]
input = "I said, \"Hi what's up?\""
input = 'I said, "Hi what\'s up?"'
I will tell some things in addition to what #Darius told above
When you tried to add a quatation mark inside a string, the lua interpreter get confused and break your string after the next quation mark without reaching the end of the line. That's the reason for the error.
Try to understand it by the following code
str = "Hello I"m somebody" -- here the interpreter will think str equals to "Hello I" at first, and then it will find some random characters after which may make it confused (as m somebody is neither a variable nor a keyword)"
-- you can also see the way it got confused by looking at the highlighted code
--What you can do to avoid this is escaping the quotes
str = "Hello I\"m somebody" -- here the interpreter will treat \" as a raw character (") and parse the rest.
You can also use the escape character () with others such as \', \", \[, \n (newline character), \t (tab) and so on.
-- 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'm new to lua and I'm trying to print an open curly bracket { in the output screen. I tried these ones:
print "%{"
print "\u123"
print "{{"
And there was no luck, but if I try the close bracket there's no problem and it prints }.
I'm wondering how may I figure it out.
print "{" Should do the job.
#lhf already sugested.
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
This question already has an answer here:
Anyone can comment this ruby code? [closed]
(1 answer)
Closed 8 years ago.
I don't know ruby language. I was reading a very interesting article which contain a following 2 line ruby code which i need to understand.
(0..0xFFFFFFFFFF).each do |i|
puts "#{"%010x" % i}"
end
By googling, i get the 1st line. But i am not able to understand 2nd line. Can someone please explain its meaning?
puts "#{"%010x" % i}" has actually two parts - string interpolation (which G.B tells you about), and string format using %:
Format—Uses str as a format specification, and returns the result of
applying it to arg. If the format specification contains more than one
substitution, then arg must be an Array or Hash containing the values
to be substituted. See Kernel::sprintf for details of the format
string.
"%05d" % 123 #=> "00123"
"%-5s: %08x" % [ "ID", self.object_id ] #=> "ID : 200e14d6"
"foo = %{foo}" % { :foo => 'bar' } #=> "foo = bar"
So "%010x" % i formats the integer in hex format (x) with at least 10 digits (10), padding with zeros (the leading 0):
"%010x" % 150000
# => "00000249f0"
Actually
puts "#{"%010x" % i}"
is exactly the same as
puts "%010x" % i
since the interpolation simply puts the resulting value (a string) within a string....
Puts key word is used to print the data on the console.
for example
puts "writing data to console"
above line will print exact line to the console "writing data to console"
#a = "this is a string"
puts #a
this will print "this is a test string"
puts "My variable a contains #{#a}"
this will print "My variable a contains this is a string" and this merging technique is called string interpolation.
this first argument in puts "#{"%010x" % i}" specifies the format and second represents the value.
and for your exact question and further details see this link
it's string interpolation and sprintf
documents:
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation
http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-sprintf
http://batsov.com/articles/2013/06/27/the-elements-of-style-in-ruby-number-2-favor-sprintf-format-over-string-number-percent/
"%010x" % i is same as sprintf("%010x", i)
puts "#{"%010x" % i}"
This line print the content. and if you want o interpolated the string please used single quote inside the double quote. like this "#{'%010x' % i }"
and %x means convert integer into hexadecimal and %010x means make it 10 place value means if out is 0 then make it like this 0000000000.
Print
puts is the equivalent of echo in PHP and printf in C
When included in either a command-line app, or as part of a larger application, it basically allows you to output the text you assign to the method:
puts "#{"%010x" % i}"
This will basically print the contents of "#{"%010x" % i}" on the screen - the content of which means that ruby will output the calculaton of what's inside the curly braces (which has been explained in another answer)