Log In  

I am understanding that you can tell a 'variable' to mimic a command, thus:

r=rnd
print(r(6))

I am wanting to know how to code this, preferably using only one 'variable' and without making it a function:

a=rnd
b=flr(a)+1
print(b(6))

How would you do it ?

P#28951 2016-09-21 13:38 ( Edited 2016-09-22 18:00)

This is how I'd do it but I use a function. Not sure why you want to avoid that.

a = function(x) return flr(rnd(x))+1 end
print (a(6))
P#28952 2016-09-21 14:54 ( Edited 2016-09-21 18:54)

That's one approach, Rytrasmi. But isn't that the same as this ?

function a(x)
return flr(rnd(x))+1
end
print(a(6))

I was seeing if it was possible to combine two commands in one without building or defining a function.

P#28989 2016-09-21 22:27 ( Edited 2016-09-22 02:27)

Not at all. "r=rnd" just makes a new handle to the function. ("commands" are functions.)

and from the lua-users wiki:
in Lua functions are regular values (like numbers, strings, tables, etc.), and you can do anything with them that you can do with any other value. This is very different from many other languages that you might know (like C), where functions have permanent names fixed at compile-time, and can't be manipulated like values.

P#28990 2016-09-21 22:54 ( Edited 2016-09-22 03:00)

Hi Tyroney.

Thanks, that helps. Really a curious language. The actual length (in chars) of the current commands are pretty small so there would be no need to reassign them (except for tweets maybe).

But there is no way to say:

r=flr(rnd)

P#28993 2016-09-21 23:04 ( Edited 2016-09-22 03:04)

flr(rnd) returns a value, so that line is processed as "r=[the value returned by flr(rnd)]"

You can't #define a macro that gets substituted at runtime.

P#28995 2016-09-21 23:13 ( Edited 2016-09-22 03:13)

Okay, I DID get some interesting values with a function I wrote recently. One to give you a random number within a range. But reverse it ? Hoo boy, what a mess !

-- get random # a to b
function fnrr(a2,b2)
local a=a2 local b=b2
  if (a>b) a=b2 b=a2
  return a+flr(rnd(b-a+1))
end--fnrr

Without that line to check to see if the numbers are reversed, you get all kinds of nasty numbers coming back if the first range is greater than the ending ! :)

The command SWAP would be nice too so I don't have to create 2-new variables in the routine above.

Also is there a way to say: local a,b without typing the word LOCAL for each variable declared as local ?

An abbreviation of FUNCT for FUNCTION would be nice if it took both. Would like to have option for 60fps with FLIP() and ... that's it for the notes I've made this time.

P#29002 2016-09-21 23:58 ( Edited 2016-09-22 04:08)

quick swap lua style:

a2,b2 = b2,a2

edit: I think if you drop in a stub for _update60() it'll shift it into high gear for your flip()s, but only on machines that support it.

edit edit: local a,b = a2,b2 (but you don't need that if you use the handy swap above)

edit^3 - just like you can pass a list of values to a function with commas, you can declare local/assign value with a list of commas. (it's tab^H^H^Hturtles all the way down!)

P#29004 2016-09-22 00:15 ( Edited 2016-09-22 04:28)

By a stub, you mean a dummy argument, right ?

function _update60()
end

Nope. Placing this at the top of the code Doesn't make the code run any smoother or faster.

Yep, that fixed it - nice. (a,b=b,a)

I noticed you do not get an error with this as well:

function addup(a)
  return a+1
end
addup(2,3)

Yes, the comma separating the variables for LOCAL works. I was thinking it was spaces.

I'm - not sure what you mean by TAB ^H^H^H turtles ?

Also, is there a CTRL key to repeat a search ? I can use CTRL-F, but what repeats the search ?

Could add that PICO could save your code automatically to _backup.p8 or something like that every 5-minutes so if you have a power out or something, you won't lose too much work. GFA had a feature like this.

P#29006 2016-09-22 00:42 ( Edited 2016-09-22 04:45)

Reading the manual reveals that ctrl+g continues a search after ctrl+f is used the first time.

P#29014 2016-09-22 02:32 ( Edited 2016-09-22 06:32)

Searching ... Actually, that's not in the main instrux is why I couldn't find it.

That's in the list of "changes." and listed nowhere else. GRIN

Okay, I guess I'll have to look in the total list of changes and updates to see if there is any instructions revealed that's not been included in the main dox.

Thanks, Springogeek.

Update:

Actually, it's there, but I was looking for "CTRL+G" Zep has it listed two ways, "CTRL-G" and "CTRL+G" That was confusing me in my notepad text search.

P#29059 2016-09-22 14:00 ( Edited 2016-09-22 22:32)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-16 19:58:35 | 0.009s | Q:24