This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
davidgumazon's profile picture

I Need Help , Text Processing and [for state do body end]

Started by davidgumazon, 29 December 2014 - 06:38 AM
davidgumazon #1
Posted 29 December 2014 - 07:38 AM
Hi Guys I Need Help for my Scripting or Coding Process

I'm Confuse in Text Processing i'm not saying this code

var1 = {[table]}
var2 = var1[num]
because it's works fine! but i'm trying each character in 1 line
example :
var1 = "asdghsgdhasd"
i want to get line 4 it's "g" so the return is "g"
if i want to get line 7 and it's "g" again! so the return is "g" again -_-/>

i'm Pro Basic in Scripting i'm confuse in Advance Scripts Like
for [???] do [body] end , i don't get it the Condition please i need Example
for you guys this Simple Script it's easy to you but me not D:
SquidDev #2
Posted 29 December 2014 - 08:58 AM
As in:

local something = "HELLO"
local third = something:sub(1,1) -- First character
local first = something:sub(3,3) -- Third character

The programming in lua book and this page are useful.
Edited on 29 December 2014 - 08:02 AM
valithor #3
Posted 29 December 2014 - 09:07 AM
A few examples of for loops

-- i is a variable, that can be named anything
-- what you set it to is the number the loop will start at (in this case 1)
-- the number following the comma is the number the loop will go to (increasing i by one everytime)
-- This for loop would print the numbers 1-5
for i = 1, 5 do
  print(i)
end
-- this one is the same as the one above, but starts at a different number
-- this for loop would print the numbers 3-8
for i = 3, 8 do
  print(i)
end

How you can apply this to the code you are wanting to do.

var1 = "this is a string"
var2 = {}
for i = 1, #var1 do -- #var1 gets the length of the string var1
  var2[i] = var1:sub(i,i) -- setting the i index in the table to the i character in the string
end

The above code will seperate a string and insert each character into a table. Although this is how you would do what you were originally asking to do, it might be easier to just use string.sub anytime you wish to get a specific character.
Edited on 29 December 2014 - 08:10 AM
davidgumazon #4
Posted 30 December 2014 - 07:19 AM
SquidDev thank you very much it's Very Useful ^_^/>
valithor thank you so much i get it how for state do body end works ^_^/>

if i get more knowledge about Lua i'll be Advance Pro in Lua if i continue this.. since i have skill in scripting or coding in
Lua,GML,CCL,OCL,CS2DL,C,C++
but my Priority Language is Lua,GML,CCL,OCL
i'm planing to create a map for ALL, DGU World is the map name + Easter Egg in the map and the map is not Adventure Map but Mods inside the map

this is my Project and this is for Computercraft :)/>

BIOL is my Best API Library but Math API inside maybe it's Confusing Code and 100% No Errors and Huge File Size but Compatible to Def-OS,Def-DOS,Mod-OS,Mod-DOS ComputerCraft Capacity System and for DOS you need Disk Drive Block with Diskette
OpenL is my Seperated API from BIOL API so the computer capacity will not eaten more
reShell returns Shell's Commands just a Simple API
UniOS it's a Operating System that is Compatible to all OS from Computercraft Forum

Super Computer i Collecting All Made from Computercraft Forum and it's not stealing :(/> ok!? and the OS for Super Computer is UniOS
Lab Computer is my Center of my Computer and it's connected to Super Computer and it's Disk Drives & Monitors
Smart LCD Computer is my Biggest and Huge CComputer Project

BTW this all my project is in Outer Space from Galacticraft 2 Mod and i'm just saying my plan
davidgumazon #5
Posted 15 January 2015 - 01:48 PM
Does

[variable]:sub([num],[num])
Really Exists ?
KingofGamesYami #6
Posted 15 January 2015 - 04:04 PM
If you mean can you use the string functions as if the string were an object, yes.

It only works on a variable holding a string though, if you tried to do this:

local var = true
var:sub( 1, 1 )

It would error.

That is to say, anything you can do like this:

string[var]( str, ... )
you can do like this:

str:var( ... )
Edited on 15 January 2015 - 03:09 PM
davidgumazon #7
Posted 16 January 2015 - 10:06 AM
i try using

[b]var:sub([num],[num])[/b]
on Coding Ground , but doesn't work , i notice this sub only works on Lua Version 5.4 and that Coding Site is using Lua v5.2
what is Computercraft's Current Lua Version Used ?

KingofGamesYami says :


It only works on a variable holding a string though, if you tried to do this:
local var = true
var:sub( 1, 1 )


Convert boolean into Number :lol:/>

local var = true
local var = fname.converter(var)
new_var = var:sub([num],[num])
Bomb Bloke #8
Posted 16 January 2015 - 10:23 PM
ComputerCraft uses a LuaJ build based on 5.1.

It supports :sub() (which is short for substring, not subtract, if that's what you're thinking), and so does Coding Ground. Neither, on the other hand, have a definition for "fname" built in.

For example:

local myVar = "Whatever"
print(string.sub(myVar,1,4))  --> What
print(myVar:sub(5,8))         --> ever

See this guide for more info.

If I wanted to convert a boolean into a number, I guess I'd do this:

myVar = myVar and 1 or 0  -- Sets myVar to 1 if it was true, or to 0 if it was false.
davidgumazon #9
Posted 17 January 2015 - 08:47 AM
ComputerCraft uses a LuaJ build based on 5.1.

It supports :sub() (which is short for substring, not subtract, if that's what you're thinking), and so does Coding Ground. Neither, on the other hand, have a definition for "fname" built in.

For example:

local myVar = "Whatever"
print(string.sub(myVar,1,4))  --> What
print(myVar:sub(5,8))		 --> ever

See this guide for more info.

If I wanted to convert a boolean into a number, I guess I'd do this:

myVar = myVar and 1 or 0  -- Sets myVar to 1 if it was true, or to 0 if it was false.
oh Cool :o/> sub() supports ComputerCraft Language now i will never get Problem for Get SubString
myVar = myVar and 1 or 0
will it work ? for me i need to do this :huh:/>

myVar = false
if myVar == true then
myVar=1
else
myVar=0
end
KingofGamesYami #10
Posted 17 January 2015 - 01:56 PM
the and / or operators are pretty cool. Try running a few of these in the lua prompt to understand how they work:


true and "hi" or "bye"
false and "hi" or "bye"
true and true
true and false
false or true
false or false
Bomb Bloke #11
Posted 17 January 2015 - 10:44 PM
http://lua-users.org/wiki/TernaryOperator