Print statement not printing after while loop - printing

Why is the print statement not working when I run this code? If I put a print statement in the while loop, it prints. However, after the while loop it doesn't work. enter image description here

You are never exiting out of the while loop.
scanner.hasNextInt() is still checking for the next integer, and if you were to input a non integer, you will that the print statement outside of the loop will execute.
I would use a different exit condition

Related

Lua io.write() adds unwanted material to output string

When I start an interactive Lua shell, io.write() adds unwanted material after the string I want it to print. print(), however does not:
[user#manjaro lua]$ lua
Lua 5.4.2 Copyright (C) 1994-2020 Lua.org, PUC-Rio
> io.write('hello world')
hello worldfile (0x7fcc979d4520)
> print('hello world')
hello world
And when I use io.write() in a program it works fine too:
--hello.lua
io.write('hello world\n')
print ('hello world')
Output:
[user#manjaro lua]$ lua hello.lua
hello world
hello world
I'm using Manjaro Linux on a Dell desktop. Can anyone tell me what's going on here? Thanks in advance.
EDIT: I should add, perhaps, that the unwanted material is always something like this:
file (0x7f346234d520)
It's always 'file' followed by what looks like a large hexadecimal number in parentheses. The exact number stays constant within one shell session but varies between different shell sessions.
"file (0x7fcc979d4520)" (or whatever address) is the return value of the io.write call, with an implicit tostring.
The lua(1) man page says
In interactive mode, lua prompts the user, reads lines from the standard input, and executes them as they are read. If the line contains an expression or list of expressions, then the line is evaluated and the results are printed.
The trouble here is that io.write('hello world') could be either an expression or a statement. Since it's a valid expression, the interpreter outputs that unwanted return value.
As a workaround, try adding a semicolon:
> io.write('hello world\n');
hello world
Although Lua usually doesn't require a semicolon for each statement if it's at the end of a line, it does allow it. And important here, it means the syntax can't be an expression, only a statement which calls the function. So the interpreter won't output the returned value.
You are just seeing the return value of io.write when you call io.write manually, interactively. When using the Lua, uh, shell, if you want to call it that, it almost always prints the return value of any function(s) you call.
file(blabblah) is the internal representation of the file you are writing to (probably just a hex memory address, but who knows?)

How to evaluate a buffer and print everything into another?

I've searched for hours on this and I have given up. Maybe you can help.
All I want to do is evaluate the LISP in a buffer and see everything that is printed. For example this here:
(setq NUMBERS (list 1 2 3))
(add-to-list 'NUMBERS 4 t)
(print NUMBERS)
So I did M-x eval-buffer and I don't see the Numbers printed. (Something not happening when using M-x M-e instead, but I don't want to this for every line and I also don't want to mark the region every time. I just want to eval the whole buffer). I looked up the description of eval-buffer and see that there is an argument for printing, but I can't give it any value because I don't know what to actually type in there. "print" maybe? But (universal-argument) only provides numerical values, so I'm lost.
Basic question: How to evaluate a whole buffer and actually see what is printed?
This makes output appear in echo area for me in Emacs 27.1:
(print NUMBERS t)
even though the docs say:
Optional argument PRINTCHARFUN is the output stream, which can be one
of these:
- a buffer, in which case output is inserted into that buffer at point;
- a marker, in which case output is inserted at marker’s position;
- a function, in which case that function is called once for each
character of OBJECT’s printed representation;
- a symbol, in which case that symbol’s function definition is called; or
- t, in which case the output is displayed in the echo area.
If PRINTCHARFUN is omitted, the value of ‘standard-output’ (which see)
is used instead.
and standard-output is:
Its value is t

Why does the Lua REPL require you to prepend an equal sign in order to get a value?

I need help turning off this feature if possible from the interactive mode or I'm going to go mad. The REPL insists on an equal sign before every expression if you want the value. I find this very irritating and unintuitive. To make matters worse, if you mistakenly forget the equal sign, it takes you to this secondary prompt which can only be exited by
typing an expression that'll cause an error.
*** str="This is some string"
*** str
>>
>>
>> =
>>
>> =str
stdin:6: unexpected symbol near '='
*** =str
This is some string
*** #str
stdin:1: unexpected symbol near '#'
*** =#str
19
***
*** 545+8
stdin:1: unexpected symbol near '545'
*** =545+8
553
***
I need a lesson in using the REPL:
Is there a way to get rid of the equal sign so that it behaves like other REPLs?
How do you exit from the secondary prompt without doing what I did?
Everything you enter in standalone Lua is treated as a statement, as opposed to an expression. The statements are evaluated, and their results, if any, are printed to the terminal. This is why you need to prepend = (really shorthand for return) to the expressions you gave as example to get them to display properly without error.
The "secondary prompt" you are seeing is what happens when you enter an incomplete statement.
In interactive mode, if you write an incomplete statement, the interpreter waits for its completion by issuing a different prompt.
You exit from it by completing the statement.
However, it's not too difficult to make your own REPL that does what you want. Of course, you lose the ability to progressively build statements from incomplete chunks this way, but maybe you don't need that.
local function print_results(...)
-- This function takes care of nils at the end of results and such.
if select('#', ...) > 1 then
print(select(2, ...))
end
end
repeat -- REPL
io.write'> '
io.stdout:flush()
local s = io.read()
if s == 'exit' then break end
local f, err = load(s, 'stdin')
if err then -- Maybe it's an expression.
-- This is a bad hack, but it might work well enough.
f = load('return (' .. s .. ')', 'stdin')
end
if f then
print_results(pcall(f))
else
print(err)
end
until false
Since Lua 5.3, you don't need the =, because Lua first tries to interpret it as an expression now.
From the reference manual:
In interactive mode, Lua repeatedly prompts and waits for a line. After reading a line, Lua first try to interpret the line as an expression. If it succeeds, it prints its value. Otherwise, it interprets the line as a statement. If you write an incomplete statement, the interpreter waits for its completion by issuing a different prompt.
A little test:
Lua 5.3.0 Copyright (C) 1994-2014 Lua.org, PUC-Rio
> str = 'hello' .. ' Lua'
> str
hello Lua
> 1 + 2
3
>

MIPS macro function. How to print out the value in register from $s` to $s7

I know how to print the value in one register.
I am wondering instead of using 8 print statement print $s0 to $s7, is that anyway i can use loop to do this?

Lua string.gsub without printing match count

Frustratingly, any my previous Lua tries went in extensive Google searching of more/less same Lua resources, and then resulted in some multi-line code to get basic things, which i.e. I get from Python with simple command.
Same again, I want to replace substring from string, and use i.e.:
string.gsub("My string", "str", "th")
which results in:
My thing 1
I imagine replacement count can be useful, but who would expect it by default, and without option to suppress it, but maybe I miss something?
How to print just string result, without counter?
Enclose in parentheses: (string.gsub("My string", "str", "th")).
The results are only a problem because you are using print, which takes multiple parameters. Lua allows multiple assignments, so normally the code would look like
newstr, n = string.gsub("My string", "str", "th")
but the count is only provided if there is a place to put it, so
newstr = string.gsub("My string", "str", "th")
is also fine, and causes the count to be discarded. If you are using print directly (the same applies to return) then you should enclose the call in parentheses to discard all but the first result.

Resources