Operations inside variable bookmark = [(1).times {puts "<||>"}] - ruby

bookmark = [(10).times {print "<||>"}]
puts "\n#{bookmark}"
This is what I can see when printing this variable.
$
<||><||><||><||><||><||><||><||><||><||>
[10]
How could I do that this will be printing the correct operation inside of the variable bookmark
Edited: Let's change the number of times to 10. I will like to be able to use the result of that variable any time that I recall it
Thank you.

So what you're doing when you do this:
bookmark = [(10).times {print "<||>"}]
puts "\n#{bookmark}"
Is you are creating a variable named bookmark. Then you are setting it to an array, with one element. Te element is: (10).times {print "<||>"}. What that does is take the integer 10, and then loops 10 times and prints <||>. It then returns itself which is the integer 10. If you want an array with ten values, each of them being "<||>", then you need to do something a little bit different.
You can multiply arrays by an integer to increase the amount of the elements you multiplied.
bookmark = ["<||>"] * 10 will set bookmark to ["<||>", "<||>", "<||>", "<||>", "<||>", "<||>", "<||>", "<||>", "<||>", "<||>"]. If when you puts "#{bookmark}" you want each of those elements to be on it's own line, you shouldn't add a newline in front (\n), but you can join the array to form a string, and you can separate each element with a newline: puts bookmark.join("\n").

Related

Why isn't Lua reevaluating io.read("*n")?

I have some code
::redo::
io.write("input: ")
var = io.read("*n")
if var then
if var > 5 and var < 10 then io.write("yes\n") goto redo
else io.write("invalid\n") goto redo end
else io.write("invalid\n") goto redo end
that is supposed to check a numeric input value and return if it's within a certain range. If it isn't a numeric value, it's supposed to "redo" the script and ask for input again. The issue is that whenever it takes an input that isn't a number it repeats io.write("input: ") and io.write("invalid\n") unceasingly meaning it's skipping the var = io.read("*n") line. Is there a special meaning or quirk to io.read("*n") that keeps it from reevaluating? The code seems to work if replaced with io.read()
When you call io.read('*n') and it doesn't find a number, it doesn't use up the input, and any calls to io.read('*n') will read the same input over and over. You need to eat up the input and discard it by calling io.read('*l'). That will let you read new input with io.read('*n').
Another method would be to read a line with io.read('*l'), extract a number from it with string.match and convert it to a number with tonumber. Then you don't have to read the same input twice, but you'd have to decide what types of number notation you want to match. (io.read('*n') accepts various types of numbers, including hexadecimal and scientific notation.)

Take the user input of numbers and print equivalent in the form of a -

I'm trying to take the user's input (numbers separated by commas, e.g., "5,8,11"), and return the equivalent number of "-"s. For example, if the user inputs "4,2,4,5", then the output should be the following:
----
--
----
-----
with each on a new line. I need to take an input string, split it at the commas, which will turn it into an array, and then iterate through the array and print the amount of commas per element.
I tried this,
puts "Enter some numbers"
input = gets.chomp
input.split(',')
input.each do |times|
puts "-" * times
end
which returns a noMethodError. I'm not sure where I am wrong.
Any help would be greatly appreciated.
You need integers for that. Try
input = gets.chomp.split(',').map(&:to_i)
Couple of things...
input.split(',')
This DOES split input, but it doesn't change the contents of the input variable.
What would work...
input = input.split(',')
Secondly, the result will be an array of strings, not integers, so better would be...
input = input.split(',').map(&:to_i)
This will map the string array into an integer array

Automatically increment filename VideoWriter MATLAB

I have MATLAB set to record three webcams at the same time. I want to capture and save each feed to a file and automatically increment it the file name, it will be replaced by experiment_0001.avi, followed by experiment_0002.avi, etc.
My code looks like this at the moment
set(vid1,'LoggingMode','disk');
set(vid2,'LoggingMode','disk');
avi1 = VideoWriter('X:\ABC\Data Collection\Presentations\Correct\ExperimentA_002.AVI');
avi2 = VideoWriter('X:\ABC\Data Collection\Presentations\Correct\ExperimentB_002.AVI');
set(vid1,'DiskLogger',avi1);
set(vid2,'DiskLogger',avi2);
and I am incrementing the 002 each time.
Any thoughts on how to implement this efficiently?
Thanks.
dont forget matlab has some roots to C programming language. That means things like sprintf will work
so since you are printing out an integer value zero padded to 3 spaces you would need something like this sprintf('%03d',n) then % means there is a value to print that isn't text. 0 means zero pad on the left, 3 means pad to 3 digits, d means the number itself is an integer
just use sprintf in place of a string. the s means String print formatted. so it will output a string. here is an idea of what you might do
set(vid1,'LoggingMode','disk');
set(vid2,'LoggingMode','disk');
for (n=1:2:max_num_captures)
avi1 = VideoWriter(sprintf('X:\ABC\Data Collection\Presentations\Correct\ExperimentA_%03d.AVI',n));
avi2 = VideoWriter(sprintf('X:\ABC\Data Collection\Presentations\Correct\ExperimentB_002.AVI',n));
set(vid1,'DiskLogger',avi1);
set(vid2,'DiskLogger',avi2);
end

Why doesn't iterating over each index of a string not function correctly?

I'm trying to perform an operation on each character of a string which is followed by another character, i.e. one that is not the last character. My code is:
[0..(str.length - 2)].each do |index|
// do something with index
end
The code errors out. [0..(str.length - 2)] returns a range object rather than an actual range which can be iterated over. Putting a return index in the body of the above causes a single iteration, with an output of something like [0..7]. Why isn't this iterating over each number?
By enclosing the range in [..], you have created an Array containing a single Range object. In other words, the length of your Array is one. You just want:
(0..(str.length - 2)).each do |index|
// do something with index
end
What you've got there is an array containing a Range rather than a single range, so each is yielding you that one range rather than the integers in that range. Replace the [] with () and you should be ok.
It should be:
(0..(str.length - 2)).each do |index|
// do something with index
end

ASP Left function issue

I have this code and something odd happening when I'm running it.
I have field number like 101512 up to 101520. I've used LEFT function to get rid of last two digits and keep the 1015. When i runn loop function for the first one it gives me 1015 but for the rest it gives me 101 an it elminates the last digit like this:
d = Split(Request("field"),",")
For i = 1 To UBound(d)
Responce.Write(Left(d(i),4))
Next
Results
1015
101
101
101
...
Does anybody have any idea what is going on?
My guess is that Request("field") may be returning a string like the following:
101520, 101521, 101522
Note the space after each comma. Thus when you apply Left() and print the value to your HTML output you don't notice the space but you only see three digits as the space counted as the first digit
One thing to try to see if this is the case is to change the code to the following:
Left(Trim(d(i)), 4)
That way any spaces around the value are removed before Left() is applied.
Correct way to iterate over "multi value" request item is actually:
For i = 0 To Request("field").Count-1
Response.Write(Request("field").Item(i) & "<br />")
Next
This will iterate the actual values without using split at all..

Resources