Create a Genie Array - arrays

Hi I have a Problem with Arrays.
I have class Arr the var a = new array of int[100] is the problem.
The example works but wenn I put the var a ... behind class Arr (return) (Tab) i have this error message :
arr.gs:3.2-3.4: error: syntax error, expected declaration but got `var' with previous `tab indent'
What's the problem. Sorry for my english , sorry I don't understand the editor
thanks
class Arr
def arr_test()
var a = new array of int [100]
i : int = 0
for i = 0 to 99
a[i] = i
for i = 0 to 99
print "%4d",a[i]
init
Intl.setlocale()
var v = new Arr()
v.arr_test()

I have tried your code in my computer and no problem, but the format of the indent was wrong.
Genie code indentation can be writed with tabs or spaces;
If you uses spaces you must to explicit how many... like this
[indent=4] at the start of the code. Like here http://manualgenie.blogspot.com.es/
But if you want to use tabs instead spaces (is more confortable) you must to ensure there is not spaces before any code line. Like here: http://genie.webierta.skn1.com/wiki/colecciones
For Vala/genie programming I use Geany editor and it has an option for replace all spaces in tabs, or all tabs in spaces in "Document" option of the task bar.
When the problem is how to use o where use "var" I will explain here:
Var is used for declare and define one identifier(variable) in only one code line and for be used temporaly. But if you want to have global scope into the class, getting it useful for all the "def" procedures of the class, you must declare at the start of the class. Like the example above. Also, if we use "init" for declare it the class must be defined as "GLib.Object"
class Arr:GLib.Object
a : array of int [] //declare
init
a = new array of int [100] //define
def arr_test()
i : int = 0
for i = 0 to 99
a[i] = i
for i = 0 to 99
print "%4d",a[i]
init
var v = new Arr()
v.arr_test()
Aslo, you can declare it but define after in your "def" procedures. Like in this example:
class Arr
a : array of int []
def arr_test()
a = new array of int [100]
i : int = 0
for i = 0 to 99
a[i] = i
for i = 0 to 99
print "%4d",a[i]
def arr_test2()
a = new array of int [120]
i : int = 0
for i = 0 to 119
a[i] = i
for i = 0 to 119
print "%4d",a[i]
init
var v = new Arr()
v.arr_test()
v.arr_test2()
Note: In this case we don't use "init", therefore is not needed the declaration :GLib.Object.

Thanks,
my Problem is not the tab or space. I have checked this.
This is the Problem :
class Arr
var a = new array of int [100]
def ...
init
...
... and so on.
It is one Tab no space.
Thanks

Related

Swift. Sort array of struct

I need to order an array of struct.
I've try:
let aRes = self.aSoundTracks_Filtered.sort{ $0.st < $1.st }
provide error: Cannot invoke 'sort' with an argument list of type '((_, _) -> _)'
also try this:
let hasPrefixAndSuffixw = self.aSoundTracks_Filtered.sort( $0.st < $1.st )
provide error: Anonymous closure argument not contained in a closure
Any idea? :)
My aSoundTracks_Filtered was delared like this:
var aSoundTracks_Filtered = [SoundTrack]()
My struct was like this:
struct SoundTrack {
let sID : Int
let st : String
}
Your code works fine when you tested in a Playground in the following way:
struct SoundTrack {
let sID : Int
let st : String
}
var aSoundTracks_Filtered = [SoundTrack]()
aSoundTracks_Filtered.append(SoundTrack(sID: 1, st: "a"))
aSoundTracks_Filtered.append(SoundTrack(sID: 2, st: "b"))
aSoundTracks_Filtered.sort{ $0.st > $1.st } // [{sID 2, st "b"}, {sID 1, st "a"}]
But sort() sorts an array in-place. What you probably want to use is sorted(), which does not modify the original array and returns a new sorted array:
let aRes = aSoundTracks_Filtered.sorted{ $0.st > $1.st }
The above code is for Swift 1.2, for Swift 2.0 returning a sorted array is called "sort" again, but it is a (protocol extension) method now instead of a global function. I hope this help you.
So, it's actually pretty simple. However I wonder where the selfcomes from when you access the array. I don't know which class it belongs to, in case it would belong to the struct itself (I wouldn't know why but just in case) you'll have to mark the function as mutating as you're changing the value of a struct's attribute. The second thing is actually that you'll have to use curley brackets:
self.aSoundTracks_Filtered.sort({$0.st < $1.st})

Flash multidimensional array of movieclips

Programming an educational program with fractions, you can touch a part of a fraction on a digiboard (or pc screen) that becomes colored/active. Touch or click it a second time and it becomes white/unactive again. And so on...
To reset a fraction with all of its colored parts to white I want to press a single button once that's calling the function resetFraction. Please take a look at the code below, it seems to go wrong at the two-dimensional array with movieclips - no change from colored to a white part. All arrays containing names of movieclips:
//array with fractions containing the (real) movieclips of parts
//mc_frac1 has one part, mc_frac2 has two parts, and so on
var fractionAr : Array = new Array(mc_frac1, mc_frac2, mc_frac3, mc_frac4, mc_frac5);
//array with max-index for partAr, see below
var maxPart : Array = new Array(1,2,3,4,5);
//array with parts, 2 dimensional with a variable index per fraction
var partAr : Array = new Array(
new Array(mc_part1_1),
new Array(mc_part2_1, mc_part2_2),
new Array(mc_part3_1, mc_part3_2, mc_part3_3),
new Array(mc_part4_1, mc_part4_2, mc_part4_3, mc_part4_4),
new Array(mc_part5_1, mc_part5_2, mc_part5_3, mc_part5_4, mc_part5_5));
//put all parts back to 'untouched' (color white)
function resetFraction(var FracNum : Number) {
var p : Number = FracNum;
for (var i = 0; i < maxPart[p]; i++) {
fractionAr[p-1].partAr[p-1][i].gotoAndStop(1);
}
}
This code below is split up in portions to see where it goes wrong:
p = 4;
mc_frac4.mc_part4_2.gotoAndStop(1); //works
fracAr[p-1].mc_part4_2.gotoAndStop(1); //works
//but
fracAr[p-1].partAr[3][1].gotoAndStop(1); //does nothing
The two-dimensional array seems to be the problem.
Anyone can help me? It saves a lot of code to realise this with one function.
Thanks a lot.
I'm out!
Instead of the dot (.) I use the array acces operators ([ ]) and quotes (" ") in the partAr array:
var partAr : Array =
new Array(
new Array("mc_part1_1"),
new Array("mc_part2_1", "mc_part2_2"), and so on...
while the fourth line in the function becomes this:
function resetFraction(var FracNum : Number) {
var p : Number = FracNum;
for (var i = 0; i < maxPart[p]; i++) {
fractionAr[p-1][partAr[p-1][i]].gotoAndStop(1);
}
}
Simple, isn't it?

How to access individual variable inside of a 2d array Actionscript 3

Im trying to access an individual tile inside of my 2d array of tiles
Ive created my array like so:
private var map:Array = [[25,25]];
ive tried several ways including:
map[1][1] = 1;
all that does is give me this:
ReferenceError: Error #1056: Cannot create property 1 on Number.
ive also tried:
map[[1,1]] = 1;
nothing happends that I can tell
The only way that ive tried so far that gets me a result thats not an error is:
map[1,1] = 1;
The issue here is this selects the whole row.
Any help would be apreciated..thanks!
This is not correct way to create 2D array:
private var map:Array = [[25,25]];
This array contains one array which contains two elements. You can't address to the second element like this:
map[1][1] = 1;
because the second element (array) of map doesn't exist.
You can create 2D array this way:
var map:Array = [];
for (var i:int = 0; i < rows; i++)
{
map[i] = [];// this line adds new row to the 2D array
// To fill the array by zeros add next loop
for (var j:int = 0; j < cols; j++)
{
map[i][j] = 0;
}
}
To start, I think that to get the error mentioned in your question, your array should looks like this :
var map:Array = [
[25, 25], // map[0] = [25, 25] ( array of two elements )
35 // map[1] = 35 ( just a simple number )
];
So, if you write :
trace(typeof map[1]); // gives : number
You will get : number, and that's why you can not right : map[1][1] = value; and it's normal that you got the #1056 error.
Here, I don't know if you meant assign the value 1 to your 2nd 25 of map[0] or you want really add or edit map[1][1], in the 1st case, you can simply write :
map[0][1] = 1; // gives : map[0] = [25, 1]
In the 2nd case, you can do :
map[1] = [any_other_value, 1]; // gives : map[1] = [any_other_value, 1]
Last remark, forget that you got an error and suppose that your map array was just:
var map:Array = [
[25, 25]
];
Here, you can not also write map[1][1] = value;, why ? Let's use the same method :
trace(map[1]); // gives : undefined
So sure you can not add a property to an undefined, that's why when you write :
map[1][1] = value;
You will get an #1010 error : "undefined has no properties. ".
Of course here, we should firstly create map[1] :
map[1] = []; // or directly map[1] = [any_other_value, value]
And then :
map[1][1] = value;
Hope that can help.

BCB6: How to put elements of a form in an array?

I'm building a simple game in C++Builder6 and I have 42 Image objects on a Form... At start-up I want all Image objects to be disabled, so I wonder can I put all of them in an array and simply loop thorough the entire array and make them Disabled? I know there must be a way, but I'm just new to programming :)
You have several options.
First: You can declare
Image* array[40];
And dynamically construct the image.
for ( int i = 0 ; i < 40; ++i ) {
image[i] = new Image(this); // where "this" is pointer to your form
image[i]->Parent = this;
// option below are optional
image[i]->Height = 50;
image[i]->Width = 50;
image[i]->Left = 40;
image[i]->Top = 100;
image[i]->Tag = i;
image[i]->OnClick = ButtonClick; // connect with method
}
Second option is declare
Image* array[40];
and manually set all values;
array[0] = Image1;
...
array[39] = Image40;
Then you will have all image in array and you can use loop for doing something on all Image

pushing or adding arrays as values into a multi-dimensional array in actionscript 3.0

I am running into some trouble adding an array into another array to create a multi-dimensional array.
The code appears as below:
var slideDataArray:Array = new Array();
var slideShowDataArray:Array = new Array();
slideDataArray[0] = xmlData.SlideShowParameters.SlideShowImagesDirectory;
slideDataArray[1] = xmlData.SlideShowParameters.SlideShowTimeInterval.valueOf();
slideDataArray[2] = xmlData.SlideShowParameters.SlideShowWidth.valueOf();
slideDataArray[3] = xmlData.SlideShowParameters.SlideShowHeight.valueOf();
slideDataArray[4] = slides.length();
slideShowDataArray[0] = slideDataArray;
for (i = 0; i < slides.length(); i++) {
// Read data from Slides tag in the XML file into slideDataArray
slideDataArray[0] = slides[i].SlideImage.toString();
slideDataArray[1] = slides[i].SlideText.toString();
slideDataArray[2] = slides[i].SlideLink.toString();
// Input the data from slideDataArray into the array for the slideshow (slideShowDataArray)
slideShowDataArray[i + 1] = slideDataArray;
}
// end of FOR loop
I am looking for a means of placing the slideDataArray into a 'slot' or value of slideShowDataArray so that I can in the end pass the slideShowDataArray as a parameter to another function.
As of now, the last slideDataArray appears 11 times (the loop runs 11 times) in slideShowDataArray and the way the code is written the slideDataArray is unique every iteration of the loop.
Any help is appreciated.
Thanks in advance...
Remember you are not adding an array, but a reference to slideDataArray to your multidimensional array. Each reference points to the same array - which just contains different values on every iteration of the loop. In other words: Every time you add that reference, you "link" to the same address in memory.
To get around this, move the inner part of the loop to a separate function and create a new local array on every call:
function createDataArray ( slide:Object ) : Array {
var slideDataArray:Array = [];
slideDataArray[0] = slide.SlideImage.toString();
slideDataArray[1] = slide.SlideText.toString();
slideDataArray[2] = slide.SlideLink.toString();
return slideDataArray;
}
Then call it from your loop:
for (i = 0; i < slides.length(); i++) {
slideShowDataArray.push( createDataArray (slides[i]) );
}
You should end up with 11 unique arrays instead of one array that is overwritten 11 times.

Resources