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

[SOLVED]Replace the 100th character in string with XYZ

Started by ArchAngel075, 21 September 2012 - 12:43 PM
ArchAngel075 #1
Posted 21 September 2012 - 02:43 PM
Currently working on a Modular Lightup TV that has realtime frame editing and eventually colour screen suipport … BUT!
I have already "finished" it but wish to improve as much as possible before i am satisfied.

NOTE : I am using an idea based on binary to define what pixels lights up in the frames.

EGs :

The pixels are actual computers with lightblocks attached to the front, when they recieve a broadcasted message containing the BINARY it will look at the nth digit thats equal to its pixel number.
IE if the computer is pixel 3 it will look at the 3rd digit, if 0 its off / if 1 its on.

to set frames and pixels i currently use :
px = 0
FRAME = {px}
FRAME[px] = "001"
print(FRAME[0])


in here the px stands for the frame number, set as 0 in EG
next FRAME[0] is set as "001"
this way if FRAME[0] is broadcasted then the computers posing as pixels will recieve "001"



BUT!

My problem is to edit that string("001") if a player decides to change the last digit to 0 using the string.gsub() won't work afaik since I can not provide it what nth character to replace.

Perhaps if there was a way to do it like so :
string.gsubN(string,with[,i][,,j]) – not sure if thats how layout would look…
where :

string | is the input string (in my case its "001")
with | is the repalce with (in my case its "0")
i | is the from indicator (in my case its 3 as its 3 characters forward | or -1 as its 1 character from the back)
j | is the end-to indicator (n my case its 3 as we want only 3 to change | or -1 as we just need the last character to change")

in full my case would be :

string.gsubN("001" , "0", 3, 3)
OR
string.gsubN("001" , "0", -1, -1)


—–
IF the code im using is required for more detail or help ill provide it, although its pretty messy and i have not used tabs or spaces yet.
Pinkishu #2
Posted 21 September 2012 - 03:00 PM
to replace the nth character with "x" i would usually use like

str = string.sub(str,1,(n>1 and n-1 or 1 ) ).."x"..(string.len(str)>n and string.sub(str,n+1) or "" )
not sure if thats the best way though :)/>/> and not sure if the extra checks are needed

(n>1 and n-1 or 1)
basically checks if n is greater than one and if so it uses n-1 and if not it uses 1; that is so it can replace the frist cahracter else it would end up being str,1,0 instead of str,1,1

(string.len(str)>n and string.sub(str,n+1) or "")
basically checks if string length is geater than n, so we only need to append something of course if there is something behind the replaced character
string: abc
string len: 3
replace char: 3
new string: abx
we didn't need to append anything after the x

but

string abcd
string len: 4
new string :abxd
here we needed to add something after it


i think theres a better way for it though :3
Fatal_Exception #3
Posted 21 September 2012 - 03:18 PM
If you're going to work in binary, you might want to give the bit API a look
ArchAngel075 #4
Posted 21 September 2012 - 06:01 PM
Thanks for the link although im only using binary as an basis to detect on off(i will eventually even change it to letters for when color screen comes in, and for that i will instead have :

Instead of 001 it will be "abb"
where :
a | tells the pixel to use WHITE wool
b | tells the pixel to use ORANGE wool

for than i can then replace the "a" with "c" (a-p for colors in their NEI order)
Thus the character replace is still needed, but i will look into what you gave for future proyects or current side proyects.

edit
didnt see pinks' reply :

Hmm confused abit, going to look into it…



atm im looking into having the intended 1024 pixal saves(0 or 1 for black/white | a to p for colours) having thier own variable in a table.
but also then saving the individual pixal values in a table for frames :
FRAME[mm] = pixal[nn]
where :
FRAME[mm] is the FRAME table | mm would indicate the frame number (ie frame[1] is frame 1)
and
pixal[nn] is the pixal table whith the nn being the pixal number

THUS a code like this
mm = 1024
pixal[mm] = a – a is white in color table(based on NEI)
FRAME[1] = pixal[mm]

the problem is how can i have the these lines also without replacing what FRAME[1] is = to :

– stating previous code to keep track
mm = 1024
pixal[mm] = a – a is white in color table(based on NEI)
FRAME[1] = pixal[mm]
– next
mm = 1
pixal[mm] = b – b is orange
FRAME[1] = pixal[mm]

thus by looking at FRAME[1] we can store all the pixals values for that frame inside it.

IS the above possible ?



LASTLY
i looked at persistant files being a good alternative to both needing rednet and data, since the n'th line in a file could be the n'th frame thus :

line 1 in FILE "FRAMES.txt" = abb
line 2 in FILE "FRAMES.txt" = aab

so the pixal posing computers look at the n'th line and z'th character where n = frame, and z = the pixal



(Wow thats alot for a grade 10 to go through :/)
ArchAngel075 #5
Posted 21 September 2012 - 06:33 PM
Sorry for double post but i was just testing pinks tip out, seems to work , im going to implement for now, Also since it works perfectly with letters which is a must for the colour part in later versions. Thank you pink :)/>/>

FURTHER tested :
Works wonderfully :)/>/> thats exactly what i needed – not sure how the hell it works :D/>/>

now i can definitly not worry bout' how to get colour detected (colour TVs are better !)