Creating AFL to replicate a python tutorial example

Been dabbling a bit with learning some python. One of the fun things I've been doing is trying to replicate some of the exercises/tutorials with AFL. I find I can read most code easy enough but trying to create AFL my brain could use some exercises.

Tonight I'm stumped on trying to replicate this code. Mind you this has no benefits whatsoever to anything I'd ever do in Amibroker but thought perhaps some of the more experienced programmers here could figure it out in a minute.

> #https://github.com/megantaylor/Learn-Python-the-Hard-Way/blob/master/ex33.py
> 
> def number_range(max, step):
> 	i = 0
> 	this_list = [ ]
> 	while i < max:
> 		print "Item: %d" % i
> 		this_list.append(i)
> 		i = i + step
> 	print this_list 
> 
> def number_range_using_for(max, step):
> 	elements = range(0, max, step)
> 	for item in elements:
> 		print "Item: %d" % item
> 	print elements
> 
> number_range(10, 2)
> number_range_using_for(20, 2)

The results look like this

Item: 0
Item: 2
Item: 4
Item: 6
Item: 8
[0, 2, 4, 6, 8]
Item: 0
Item: 2
Item: 4
Item: 6
Item: 8
Item: 10
Item: 12
Item: 14
Item: 16
Item: 18
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Normally I can kind of sift through the list of Amibroker functions and get started somewhere but I don't even have anything that even looks like code to even post here.

AFL doesn't have anything like pythons lists which are arrays so don't think an exact match would happen. Closest thing I could think of is somehow creating a string that looks like a list but then the numbers aren't numbers.

You can store to custom array in AmiBroker also (using Matrix).

function number_range(maxi, step) {
	/// py to AFL
	/// @link https://tinyurl.com/rsy2aqo
	mx = Matrix(1, maxi/step);
	i = n = 0;
	while ( i < maxi) {
		if ( i % step == 0 ) {
			printf( "Item: %g\n", i );
			mx[0][n++] = i;
		}
		i += step;		
	}
	return mx;
}

mx = number_range(10, 2); 
printf(MxToString(mx));

9


As for 2nd example using for loop...

range() is ready function there in py
So you have to re-recreate it in AFL to have same short number_range_using_for() function
e.g.

function range(mini, maxi, step) {
	/// py to AFL
	/// @link https://tinyurl.com/rsy2aqo
	mx = Matrix(1, ceil((maxi-mini)/step));
	i = n = 0;
	while ( i < maxi ) {
		if ( i % step == 0 ) {
			if ( i >= mini ) 
				mx[0][n++] = i;
		}
		i += step;		
	}
	return mx;
}

function number_range_using_for(maxi, step) {
	f_range = range(0, maxi, step);
	elements = MxGetSize(f_range,1);
	for ( i = 0; i < elements; i++ )
		printf( "Item: %g\n", f_range[0][i] );
	return f_range;
}

mx = number_range_using_for(20, 2);
printf(MxToString(mx));

9

https://www.amibroker.com/guide/keyword/while.html
https://www.amibroker.com/guide/keyword/for.html
https://www.amibroker.com/guide/AFL.html

1 Like

Beautiful. Matrix never even crossed my mind. Figured you’d have the answer quickly fxshrat.

Just realize that I have forgotten to remove if ( i % step == 0 ) in both samples.
It is not required.

So just

function number_range(maxi, step) {
	/// py to AFL
	/// @link https://tinyurl.com/rsy2aqo
	mx = Matrix(1, maxi/step);
	i = n = 0;
	while ( i < maxi) {
		printf( "Item: %g\n", i );
		mx[0][n++] = i;
		i += step;		
	}
	return mx;
}

mx = number_range(10, 2); 
printf(MxToString(mx));

as well as...

function range(mini, maxi, step) {
	/// py to AFL
	/// @link https://tinyurl.com/rsy2aqo
	mx = Matrix(1, ceil((maxi-mini)/step));
	i = n = 0;
	while ( i < maxi ) {
		if ( i >= mini ) 
			mx[0][n++] = i;
		i += step;		
	}
	return mx;
}

function number_range_using_for(maxi, step) {
	f_range = range(0, maxi, step);
	elements = MxGetSize(f_range,1);
	for ( i = 0; i < elements; i++ )
		printf( "Item: %g\n", f_range[0][i] );
	return f_range;
}

mx = number_range_using_for(20, 2);
printf(MxToString(mx));
2 Likes

Was going through the examples you provided tonight and I've come to the conclusion I got a long way to go before I ever come up with something like that. I can wrap my brain around 95% of what you did there but to come up with that, is kind of mix of science and art.

Guess when you do enough of anything and practice enough it shows.

1 Like