1. 首页
  2. 易语言 - EasyLanguage
  3. Arrays
  4. 关于数组 Understanding Arrays

关于数组 Understanding Arrays

本文运用EasyLangauage的简单例子介绍数组的概念,包括一维数组、多维数组、填充数组、打乱数组数据和循环数组。

The Array concept is very simple. 
This page will attempt to demystify the concept of Arrays within EasyLangauge using simple examples. Please feel free to edit this article and to add alternate methods that help clarify the content.

Think of an Array as an Excel spreadsheet, where you have rows and columns.
From this approach, the following examples are designed:

  • One Dimensional Arrays
  • Multi Dimensional Arrays
  • Populating Arrays
  • Shuffling Array Data
  • Circular Arrays
  • Sort new field into Array – soon

Incrementing  : For Counter = 1 to Array_Size begin… end;
Decrementing : For Counter = Array_Size DownTo 1 begin… end;

One Dimensional ARRAYS 

A one dimensional Array has one column, with X number of rows to query.

Array: myArray [10] (0);

 [10] defines one column with 10 rows (not to confuse but a 0 row is actually available too to make 11 ) 

  • [10] defines one column with 10 rows ( note that a “0” row is available to to make the total number of rows 11 ) 
  • (0) = Numeric Data
  • (Text) =  String Data can be entered

Multi-Dimensional Arrays

Multi-Dimensional arrays enable you to store vast amounts of data, similar to a spreadsheet.  This enables you to add a specific series of data per bar, or per condition, for back-testing or comparison.

Warning
The standard Array type is a two dimensional array. Three dimensional arrays are possible, but with some warnings.
A 3 dimensional array would be declared as myArray[10,10,10];  Due to excessive processor resource requirements, care should be taken when using Arrays of 3 dimensions.  The conceptualization of a three dimensional Array is similar to a spreadsheet cell hyperlinking to a different spreadsheet. A better example may be a pallet of boxes 10 across, 10 high and 10 deep. The complexity rises exponentially as new dimensions are added to an Array.

Two Dimensional Array

Array: myArray [10,10] (0);

the [10,10] defines 10 columns with 10 rows (or not counting the 0 row, 100 fields to populate with data) 

  • (0) = Numeric Data
  • (Text) =  String Data can be entered.

If you ask for:
Value1 = myArray*[6,7]*; returns the value of column 6 and row 7 intersection, 819.00 in the example below.

Populating Arrays

Arrays values are held from bar to bar.
An Array can be populated by a hard number as in:
myArray[4] = High;

or by a Counter method that you define. The variable name is arbitrary as long as the value is not higher than the declared dimensional value.

Var: Counter(0);

Counter = Counter +1;
myArray[Counter] = High;

Shuffling Array Data

Shuffling simply moves the first row of an array to the second and so forth, leaving the Arrays first row open for a new entry.
This is normally done in one step or in a procedure.

Single Dimension Array Shuffle

Array: myArray [10] (0);
Var: myArraySize(10);
Var: Counter1(0);
Var: HoldRow(0), SaveValue(0); 
SaveValue = Value1; {your value or caluclation you want arrayed} 
For Counter1 = 1 to myArraySize begin
	holdRow = myArray[Counter1];  {save the current Value of this row for the next row }
	myArray[Counter1] = SaveValue; {pass the newer Value to this row}
	SaveValue = holdRow;	   {pass the old Value of this row for the next loop}


end;

Multi-Dimensional Array Shuffle

The code below will shuffle a multi-dimensional array. A new row will be inserted in position 1 and push the remaining rows up one row.

Array: myArray [100,10] (0);
Var: myArraySize(100);
Array: Save[10](0);   {used as holders for the shuffle}
Array: Hold[10](0);   {used as holders for the shuffle}

Var: Z(0), X(0);      {Using Single Letters as counters}


{Pass your newest set of Values to a one dimensional Array}

If condition1 then begin
	{Sample Data}
	Hold[1]= Date;
	Hold[2]= Time;
	Hold[3]= Open;
	Hold[4]= High;
	Hold[5]= Low;
	Hold[6]= Close;
	Hold[7]= UpTicks;
	Hold[8]= Downticks;
	Hold[9]= CurrentBar;
	Hold[10]= close -close[1]0;

	for Z = 1 to myArraySize begin

		For X = 1 to 10 begin Save[X] = myArray [Z,X];  end;  {pass Array Row to Save Array variable LOOP}

		For X = 1 to 10 begin myArray [Z,X] = Hold[X];  end;  {pass Hold Array variables row  LOOP}

		For X = 1 to 10 begin Hold[X] = Save[X]; 	  end;  {pass the SaveArray to Hold Array LOOP}

		if Hold[1]= 0 then BREAK;	 {Get out of main LOOP if passed row /Hold Array is Empty }

	end;
end;

Circular Arrays

This is an example of a First-In-First-Out (FIFO) circular array. Using arrays in this manner is much faster then ‘shuffling’ because you are not constantly moving all the array data around. The disadvantage of this approach is that references to previous values (the equivalent of data[1], data[2], etc.) is more difficult.

//Circular Array - First-In-First-Out (FIFO)

//Code works by ReadIndex always being one bar behind WriteIndex

Inputs:	myArraySize(10);

Vars:	ReadIndex(0),
	WriteIndex(1),

	ArrayHasData(false),

	MyValue(0);

//Must declare our array size bigger than the myArraySize input
Arrays:	myArray[100](0);

//this is where you are putting your data into the array
//we test 'condition' to see if we should be putting data in this bar
if {condition} then begin
	myArray[WriteIndex] = close;

	WriteIndex = WriteIndex + 1;
	ReadIndex = ReadIndex + 1;

	ArrayHasData = true;
end;


//this is where you get your data out of the array
if ArrayHasData then begin
	MyValue = myArray[ReadIndex];
end
else begin
	//your array is currently empty
	//MyValue = -999999;  //or something similar
end;


//check for 'rollover' of our read and write index counters
if WriteIndex > myArraySize then WriteIndex = 1;
if ReadIndex > myArraySize then ReadIndex = 1;
更新于 2020年5月25日

这篇文章对您有帮助吗?

相关的话题

留言评论