Trouble with DataBuffer data

Monkey Forums/Monkey Beginners/Trouble with DataBuffer data

bitJericho(Posted 2015) [#1]
Hello, when I run this, the peeked data doesn't match what I poked.

Import mojo
Import brl

Class MyApp Extends App

	Method OnCreate:Int()
		Local testdata := New DataBuffer(3)

		For Local i := 0 To 0
			testdata.PokeByte(( i*3)   , 0)
			testdata.PokeByte(((i*3)+1), 1)
			testdata.PokeByte(((i*3)+2), 0)
			
			Print "startdebug"
			Print i
			Print testdata.PeekByte( i*3) + " = 0"
			Print testdata.PeekByte( i*3)+1 + " = 1"
			Print testdata.PeekByte( i*3)+2 + " = 0"
			Print "enddebug"
		Next
	End
End
Function Main:Int()
	Local application:MyApp = new MyApp()
	Return 0
End


My output is this. Notice the last line says 2=0! What am I doing wrong?

startdebug
0
0 = 0
1 = 1
2 = 0
enddebug



skid(Posted 2015) [#2]
if you read your code again more carefully you should notice

testdata.PeekByte( i*3)+2

is adding 2 to the result not the address of the peek


golomp(Posted 2015) [#3]
Hi BitJericho, welcome to our community,

Skid is right, your brackets are at wrong place.
*this is the corrected code:
Import mojo
Import brl

Class MyApp Extends App

	Method OnCreate:Int()
		Local testdata := New DataBuffer(3)

		For Local i := 0 To 0
			testdata.PokeByte(( i*3)   , 0)
			testdata.PokeByte(((i*3)+1), 1)
			testdata.PokeByte(((i*3)+2), 0)
			
			Print "startdebug"
			Print i
			Print testdata.PeekByte( i*3) + " = 0"
			Print testdata.PeekByte( i*3+1) + " = 1"
			Print testdata.PeekByte( i*3+2) + " = 0"
			Print "enddebug"
		Next
	End
End
Function Main:Int()
	Local application:MyApp = New MyApp()
	Return 0
End



bitJericho(Posted 2015) [#4]
Woops! Funny how those brackets will get you!


golomp(Posted 2015) [#5]
Yes, brackets should be forbiden.
RPN language is a computer language without brackets. A kind of paradise.


Paul - Taiphoz(Posted 2015) [#6]
Brackets are fine however I would love to see an Editor that draw a line under from the starting bracket to the ender for each set possibly colour coded. something like this would be really cool.

something like this would be a nice feature for an IDE.


golomp(Posted 2015) [#7]
You can avoid brackets in all your calculations formula.

for example : y=2*(x-5)
can be programmed like this:
tmp=x-5
y=5*tmp

You see ? No brackets needed. World could be so better without brackets.

I hope in the future brackets will disappear from keyboards.

Personnally i think seriously to remove them on my keyboard.


bitJericho(Posted 2015) [#8]
I like brackets but I do wish the monkey IDE had decent highlighting. I like Paul's idea. I think even better is that when you are editing that line it would get rid of the fancy highlighting and show only the underline that's inside the bracket you're working on, for example: https://www.dropbox.com/s/6ulzqjq1p4ud3ue/code1.png?dl=0


golomp(Posted 2015) [#9]
Brackets in calculation formula should disappears. Like that no need to manage brackets error.
Imagine a RPN fonction replacement :
the expression y=2*(x-5) could be written like this :
y=RPN(x,5,-,2,*)
Forth is also a language (a fourth generation one) with a RPN system. It works without brackets. It's also used in embedded systems.
Many of HP calculators work without brackets (example HP41) and this calculator has been selected by NASA for a space mission.
Brackets are obsolete.


Nobuyuki(Posted 2015) [#10]
Brackets are obsolete.


says the guy mentioning Forth and a calculator discontinued 25 years ago ;)
and now for something completely different:


Reverse Polish Sausage


golomp(Posted 2015) [#11]
Hi Nobuyuki,

That sandwich looks good.Maybe it's a solution.

I continue to use hp41 every day. (it's auto execution possibility are very usefull)

I also use WP34S wich is a community project on SourceForge

And i use also Forth (FPC 3.6) on PC and a HHC-1400 with a homemade forth module.

Not so bad for a discontinuation...

Enjoy your lunch.