1c find the value in the array. How to create a fixed array

Home / Operating systems

A 1C array is a simple set of values. You can add values ​​of different types to one 1C array. After adding, the presence of values ​​in the 1C array can be checked using the built-in search.

Array 1C is often used:
A) For selections
B) To save a list of values ​​in order to use it further, for example, pass it as a parameter to a function/procedure.

Working with 1C arrays

Let's create a 1C array of 2 values:
Array = New Array(); //array without a fixed number of values

Array.Add(RNG.RandomNumber(0, 1000));
Array.Add(RNG.RandomNumber(0, 1000));

//cycle through each value of the 1C array, no matter how many there are

Report(Value);
EndCycle;

Let's create a 1C array of 10 values:
Array = New Array(10); //array with a fixed number of values
RNG = NewRandomNumberGenerator(); //we will fill with random numbers

//array index starts from zero, not one
//10 pieces are from 0 to 9
For Sch = 0 to 9 Cycle
//access to the arrayArrayName[Index]
Array[RNG] = RNG.RandomNumber(0, 1000); //random number from 0 to 1000
EndCycle;

//loop through each array value, no matter how many there are
For each Value from Array Cycle
Report(Value);
EndCycle;

//note - there are 10 array values, this means that there are 10 of them by default and each value is UNDEFINED until we set another
//this does not mean that we cannot add another value, 11e, using ArrayName.Add();

Multidimensional array

Let's create a multidimensional 1C 2x2 array. A multidimensional array is 1C, which means that each cell of the 1C array contains not a value, but also a 1C array.

A 1C multidimensional array does not have to be fixed. Let's make a multidimensional 1C 2x2 array without specifying a fixed number of values:

Creating a 1C array from scratch

Obtaining data is a time- and resource-consuming procedure. Therefore, if you need to work with the received data more than once, it makes sense to receive it once and save it in a 1C array in order to further work with the data already received and saved.

For example, the program version line looks like this: “11.0.9.5” - four numbers separated by dots. We can translate the version into a “computer” format - an array of four numerical values. Then we can further compare the versions with each other:

Obtaining a ready-made 1C array with data

A 1C array can be obtained from a list of values ​​using the spList.UnloadValues() method and from a table of values ​​using the tableTable.UnloadColumn() method.

In the example below, we make a query - we select all document links from the database.

  • RequestResult = Request.Execute() //executes the request
  • tzResult = Query Result. Upload() //uploads the result to the table of values
  • mArray of Links = tzResult.UnloadColumn("ColumnName") //unloads a table column into an array

Obtaining a 1C array from other objects and using it as a selection in a request

When a list of documents (or a table of values) is located on the form, the user can select one line with the cursor. You can also allow multiple lines to be selected at the same time. To select multiple lines (if the form does not allow this), you need to hold down the CTRL or SHIFT key.

Add a table of values ​​to the thick client form and select DocumentList.DocumentName as its type. In its properties, select the selection mode – multiple.

We can programmatically get the list of selected strings:

If you are using a managed form, then you must first add a form attribute with the DynamicList type, and in its properties select the main table - required document. Drag the props onto the form. In the properties of the form element (!), also select – selection mode – multiple (set this way by default).

Accessing the 1C array of selected strings will look like:
Elements.FormAttributeName. Selected Rows

What is this article about?

This article continues the series of articles “First steps in 1C development.” It covers principles of working with universal collections. After reading the article, you will learn:

  • What are universal collections, and when and in what cases should they be used?
  • What do all universal collections have in common? What techniques can you use to work with all of them?
  • What is an array, how and when to use it? What methods does he have?
  • Why use a structure? How is it different from an array?
  • When should you use a list of values? How to display it on the form?
  • Compliance – what is it and when to use it? What are the advantages regarding structure?
  • What is the value table used for? How to describe its structure? How to add/remove lines? How to display it on the form?
  • Tree of values ​​- what is it used for? How to fill out and display the form? How to work with it?

Applicability

The article discusses the 1C:Enterprise 8.3 platform of the current edition.

How to work with universal collections in 1C

A collection of values ​​is a container that can usually contain any number of elements. However, there are often no strict restrictions on the data type.

You can add values ​​to a generic collection. All values ​​in the collection can be traversed. These collections are used mainly for some kind of processing in algorithms. Those. These are some dynamic structures that exist while the algorithm is running.

It is important to understand that collections are not stored in a database (we are not talking about the Value Store data type, which can store almost any type of data).

There are different types of generic collections: Array, Structure, Match, Fixed Array, Value Table, Tabular part etc. But all collections have similar behavior.

A collection can be created as a result of the operation of a function (the function returns a universal collection as a value).

Can be obtained new collection manually by accessing the constructor and creating an instance of the class.

For example: OurArray = New Array;

The constructors for many generic collections are parameterized.

So, in the constructor for You can specify the number of members in the corresponding dimensions. Those. you can immediately declare multidimensional .

The corresponding description of the constructor is in the syntax assistant.

Thus, using constructor parameters, you can immediately set the desired behavior of a given object.

But the parameters are optional; the developer can not set them and further define the behavior of the Array as he sees fit.

Almost any universal collection can be created using a constructor (the exception is table parts, which act as configuration objects).

For universal collections, there are common concepts such as index and number. Each element of the collection has an index. In this case, the index starts from zero.

To access an element OurArray, you can use index access; for this, the index is indicated in square brackets.

For example, OurArray. Please note that in this case the system returns the Array element with index 3, and in order this is the fourth element of the Array.

For some collections there is also the concept of a line number. The line number starts with one. For example, for the tabular part there is such a property as row number. It is important to keep in mind that if we know the line number and want to access by index, then the value one less than the line number should be used as the index.

The concept of a line number does not exist in all collections, but mainly in those that can be displayed in the user interface.

All collections use collection element traversal. Bypass is possible in two ways: cycle For And cycle For each of.

The methods that apply to most generic collections are Count, Index, Add, Insert, Delete, and Find.

Count is a function that returns the number of elements in a collection. It can be used before a cycle For, as shown in the figure.

The Index method does not exist for all collections, but only for those whose elements can be referenced. An example is Table of Values.

Table of Values– this is a specific collection of strings, the strings may contain different columns with different types values.

Each line represents an independent entity. You can get a link to it; through this line you can access the values ​​of the columns in this line.

The Index method allows you to determine which index corresponds to a given row (that is, the current position of the row in the table). Index values ​​start at zero.

Almost any universal collection has methods for adding new values ​​to a given collection. The figure shows how to fill an Array with values ​​from 0 to 10 in two ways.

To add an element to the Array we can use the method Add, indicate the value to be added in brackets. In this case, the value will be added to the end of the list, i.e. The array will constantly increase due to the last position.

Another method that allows you to add values ​​to a collection is the Insert. It is different from the method Add in that you can specify where to insert the added element.

Syntax: Insert (,)

The first parameter specifies the index into which the new value will be inserted. Those. for example, we can specify that each value should be inserted at the beginning of the list (second method in the figure above).

To remove elements from a collection, use the method Delete. The Delete method specifies by index which element we will delete.

Syntax: Delete()
Usage example: OurArray.Delete(5);

It should be noted that for those collections where strings represent an independent entity (for example, for Tables of Values), we can also use the method of getting the index in order to later delete this row.

Almost all collections have a method for searching for a value - Find. The value we want to find is passed to the method. Some collections allow you to set some restrictions.

For example, in Value Table You can specify the rows and columns in which you want to search.

If the value is found, this method returns the index or the specified string. If the value is not found, a value of type is returned Undefined. In relation to an Array, returns Index, or value Undefined.

Usage example: OurVariable = OurArray.Find(8);

Universal collections can be cleared very quickly, i.e. remove absolutely all elements. For this purpose the method is used Clear(), which removes Array elements, rows Tables of Values, or data from other collections.

Additional methods for Array

Method BBorder() returns the number of elements minus one. Those. if we use a loop For, then instead of the Quantity method we can immediately use the method Border().

In particular, the variable QuantityInArray could be defined differently:

QuantityInArray = OurArray.InBorder();
Then, when describing the cycle itself, one should not be subtracted from this variable.

The Set method allows you to assign a value to an Array element by index.

Syntax: Install(,)

Example: OurArray.Set(2,8);

Alternative: OurArray = 8;

You can use the method for an Array Get, for reading a value at an index without resorting to using square brackets.

Syntax: Get()

Example: OurVariable = OurArray.Get(2);

Alternative: OurVariable = OurArray;

Universal collection Structure

A Structure, like an Array, can have an unlimited number of elements, but the content of the element differs from the Array.

The structure is a collection, each value of which consists of a pair. The first element of the pair is called Key. The second element of the pair is Meaning.

Key is a strictly string data type that describes a value. For example, To the key"Code" can correspond to the value 113; To the key“Name” meaning “Vasya”. The Value itself is not subject to a data type restriction.

The structure is very convenient to use if we want to create a certain list of parameters. If this Structure called OurStructure, then we will refer to its two values ​​as follows: OurStructure.Code and OurStructure.Name.

This kind of access is much more convenient than if we defined all the parameters in an Array and accessed them by index.

Structure makes program code readable (understandable). Structure is used quite often, much more often than Array.

It is used to describe certain parameters, of which there are often quite a large number in all algorithms.

In addition, the Structure is used if the procedure and function contain a large number of passed parameters.

Then it is much more convenient to write all the parameters into the Structure and pass it on. Those. the parameters of procedures and functions are “packed”.

Separately, it should be noted that as Key Not absolutely any line can appear in the Structure. Certain restrictions apply.

Key should act as an identifier. This means that in Klyuche there must be no spaces and it cannot start with a number.

Acceptable start Key with a letter or underscore. Thus, Key must satisfy the requirements for creating identifiers.

Let us note how else a Structure differs from an Array. There is a method in the Structure Insert, there are two methods for insertion in Array: Insert(to a certain position) and Add(to the end of the list). In an Array, all elements are ordered.

Structure is a kind of disordered set. This is why there is only an insert method for a Structure.

The value is inserted not at a specific position, but into the specified set. A Structure cannot be accessed by index, like other generic collections.

Structure elements are accessed only by Key name. However, the For Each loop also works for the Structure, but you should not rely on the order of the Structure elements.

A structure is created in the same way as other generic collections by using the New constructor, specifying the data type Structure.

Like an Array, a Structure's constructor can have parameters. Those. it is possible to describe the very content of a Structure using a constructor.

Unlike an Array, where you can simply specify the number of elements for all dimensions, in a Structure you can specify the content itself.

For example: OurStructure = New Structure (“Code, Name”, 133, “Vasya”);

Separated by commas, the names of the Keys are first listed, and then, accordingly, in the same sequence, the values ​​of the parameters.

There is a method to add a new value to the Structure Insert, which inserts a new pair (Key and Value).

For example: OurStructure.Insert(“FamilyMembers”,3);

The Structure is characterized by another method that is used quite often. This is the method Property.

Using this method, you can understand whether there is an element in this Structure whose Key has such and such a name.

If such an element exists, the system will return the value True, in otherwise- Lie.

For example, the expression OurStructure.Property (“Family Members”) will be equal to the value True. This method is used quite often when analyzing the Structure.

As with any universal collection, it is possible to access the properties of a Structure by index. But the index for the Structure is a string value.

For example: Report(OurStructure[“FamilyMembers”]);

However, we should not forget that the Structure is not an ordered set of objects, which is why access by index 0, 1, 2 is unacceptable.

Generic collection List of values

ListValues is a linear list of elements of any data type.

Each element consists of several values. Schematically, a list of values ​​can be represented as a list with four columns.

First column - Mark. It has a Boolean data type and allows the user to either check or uncheck boxes.

The other column is a picture that can somehow visually represent this element, i.e. match this string with a picture.

The third column is the stored value itself, i.e. this is any type of data, and it can be different in different lines.

The fourth column is the presentation, i.e. this is a kind of string description of a given value. The view will be displayed to the user when he views this element. In this case, if the representation is not specified, the system will try to obtain representations for the element contained in this position.

ListValues– this is the object that the user can visually work with. Those. ListValues can be displayed on the form.

The user can perform some actions with it. Besides this, ListValues can be inferred independently using methods, i.e. show on the screen in some branch of the algorithm (with the exception of server code) so that the user selects some line or checks some boxes.

We'll find ListValues in the sitax assistant. Constructor ListValues not parameterized (you cannot set any default values).

There are methods such as:

  • Insert(,) ;
  • Add(,);
  • Quantity();
  • Index().

There are also special methods, for example, UnloadValues(). This creates an Array into which the list of values ​​is copied. For example:

Array of Elements = List of PriceTypes.OutloadValues();

There is also a reverse method:
ListPriceTypes.LoadValues(ArrayItems);

There are search methods:
FindByValue(); FindByIdentifier().

There is a copy method:
ListCopy = PriceTypeList.Copy();
This method is intended to make some kind of modification to the copy.

There are methods:
SortByValue();
SortByView().

Methods SelectItem(,) And MarkItems() call a modal dialog box that stops execution of the algorithm until the user closes the window.

To use these methods in configuration properties Mode of using modality must be set to Use.

Example code called from a Managed Application module:

Display this code in user mode (modal dialog).

Below ListValues used as available type data for the form details. We create a new attribute for the processing form and define its type ListValues and display it on the form.

Creating a new team Fill inGifts, transfer it to the form and define an action handler for it.

In user mode, when you click the Fill in Gifts button in the processing form, a completed list will appear.

If desired, the list can be edited: some elements can be added, some can be removed.

Versatile Collection Compliance

This collection is very similar to Structure. Just like Structure, Matching represents sets of values ​​that consist of a key and the value itself.

The main difference is that any data type can be specified as the Key, as well as for the value. In view of this feature, it is necessary to access the match value by index; the key value is indicated as the index value.

The key can be a data type other than a string. The properties and methods of working with Compliance are almost the same as those of Structure.

The Compliance Constructor, unlike the Structure, does not contain the ability to specify parameters.

Usage example:

Correspondence is convenient to use when you need to connect any two structures. For example, each row of the tabular part must be matched with a row from the table of values.
In this case, the table section row is used as the Match key and the corresponding value is indicated.

When inserting elements into a collection Match in addition to the method Insert(,) Another way to insert a value is to use the regular assignment operator.

For example: OurMatch = NewMatch;
Match = 999;

Those. if an element was not present in the collection, then it will be added using the assignment operator, and if it was present, it will be updated.

This is in contrast to Structure.

Universal Collection Table of Values

Table of Values is a table with an arbitrary number of rows and an arbitrary number of columns. The intersection can store values ​​of any data type. If necessary, columns can be typed, i.e., you can determine in which column what type of data is stored.

You can leave the columns untyped, then values ​​of different types can be stored in one column in different rows.

Differences Tables of Values from a two-dimensional Array:

  • this is an object that the user can work with (the table of values ​​can be displayed on the screen, the user can fill it out, and the entered data can then be read);
  • building indexes for quick search;
  • cloning, filling an entire column with a certain value, unloading all columns into an array.

Table of Values used as a kind of information storage buffer. Table of Values is returned and accepted as a parameter by many system methods. It is possible to build a query against the Table of Values.

So, Table of Values consists of a set of rows and a set of columns. Both rows and columns are collections.

Those. inside the collection Table of Values there are two more collections. Let's turn to the syntax assistant and find Table of Values.

Supported data types: itself Table of Values, which consists of strings. Each row is represented by a data type RowTableValues, which has its own properties and its own methods. Available Collection of Table ColumnsValues also has certain properties.

Important point! The procedure that creates Table of Values, must compile &OnServer.

Before you start working with Table of Values, you need to determine what columns it will contain (i.e. create them). Syntax:

Add(,)
(optional)
Type: String.
(optional)
Type: DescriptionTypes
(optional)
Type: String.
(optional)
Type: Number.

For example:

To call this procedure we will use the command.

In the description Tables of Values the elements of the collection are precisely RowsTableValues.

Unlike columns, which consist only of properties (Name, Type, Heading, Width), in RowTableValues there are both properties (access by column name) and methods (you can get and set the value, work with owners).

To add a new row to the table you need to use the method either Add(), or Insert(). In the second case, you should indicate at what position the required line should be placed.

To assign a value to a column, we use a dot to access the column name or index (using square brackets).

To fill out Tables of Values The following methods can be used:

Clear()– to remove all rows from Tables of Values.

FillValues(,)– allows you to fill all columns or selected columns with one value.
LoadColumn(,)– loads a column from the array.
UnloadColumn()– unloads the column into an array.

The last two methods are convenient to use when you need to transfer a column from one value table to another.

Copy(,)– allows you to create a new one based on an existing table Table of Values, and not all rows and all columns, but only some of them. Return value – Table of Values.

You can copy the structure Tables of Values. There is a corresponding method for this CopyColumns(). We'll get an empty one Table of Values with the required structure.

IN Value Table there is a method Total(). You can specify the column in which you want to sum the numerical values. In relation to the previously shown code in the Tableau, you can calculate the value: TZ.Total (“Amount”).

IN Value Table it is possible to group (collapse) numerical values ​​by identical values ​​of certain columns using the method Collapse(,).

In relation to the previously shown code in the Tableau, you can calculate the value: TK.Collapse(“Day of the Week”, “Amount”).

Table of Values can be shown on user screen so that you can perform any actions with it. But unlike ListValues from program code You can’t just bring up a table on the screen.

To display Table of Values on the screen, create a form attribute and assign a data type to it Table of Values.

After that, the resulting table should be displayed on the form.

In the form module at the end of the previously compiled algorithm (in the Procedure for Creating a Value Table), you should add:
ValueInFormData(TK, Table);

Universal collection Tree of values

a universal collection that is very similar to Table of Values. The difference from a table is that the rows of the tree can be subordinate to each other, i.e. some kind of hierarchy may be formed.

This can also be reflected on the screen. A value tree explicitly consists of a collection of rows and a collection of columns. There are two properties in the tree: Rows and Columns.

Since rows can be subordinate to each other, each row can have a Parent, as well as its subordinate rows.

Let's create the corresponding Tree command and its processing procedure.

Let's create in which there is one parent row and two subordinate rows.

Let's create the form attributes DerZn(data type – Tree of Values).

For this attribute, we will create the Year and Month columns.

Move the corresponding element DerZn on the form.

At the end Procedures TreeOnServer() let's add:

ValueInFormData(TreeZn, DerZn);

Let's check what happened in user mode.

Using the button Add you can add new lines. They can also form a hierarchy.

To traverse all the elements of the value tree, we will need to use recursion, i.e. calling a procedure from itself. For example, processing a value tree might look like this:

This concludes our first acquaintance with universal collections.

In the next article we will look at what important mechanism a developer can use to simplify access to a directory element from program code.

The concept of “array” has long been used in programming and, most often, it is understood as a certain structure in memory consisting of a sequence of elements. An array in 1C is the simplest of the universal collections of values. In addition to the array, universal collections include:

  1. List of values ​​(unlike an array, the values ​​from the list have a representation, the ability to sort and interface the collection is implemented);
  2. A table of values ​​is a collection that has columns for an extended description of the value; a similar structure can be obtained by executing a query;
  3. A tree is very similar to a table, complete with a subordination structure;
  4. Structure – dynamic set variable names and values;
  5. Matching - similar to a structure, only the variables are matched not to string representations, but to each other.

Array Methods

Programmatic creation of an array instance occurs using the New() operator (Fig. 1).

As can be seen from the above example, arrays can be created either with a fixed number of elements or without this limitation.

Before we begin discussing methods for working with an array, let's define two concepts:

  • Element index – can be represented as a serial number of a single value;
  • Number of elements – the number of elements in the collection; its determination is available using the Quantity() method.

It is important to know: in 1C, counting the number of values ​​of any collection starts from 1, and distributing indexes from 0, i.e. the first element in the array has index 0. Thus, traversing a collection using a loop with an iterator should start from 0 and end with the number of elements minus 1, otherwise the system will notify you of the occurrence of an exception using the window (Fig. 2).

Fig.2

Any configuration object has its own methods for working, an array is no exception, let’s list them with some explanation:

  • InBorder() – using the method you can get the maximum index of an element; for an empty array the value (-1) will be returned;
  • Insert() – this method has two parameters: index and value, index indicates where to insert a new element into the array, the added value can be empty;
  • Add() – this method of inserting a value can be used when the placement of an element is unimportant; with its help, new data will be written to the end of the existing array;
  • Find() – if successful, returns the index of the value specified in brackets, otherwise returns “Undefined”;
  • Clear() – clears the collection;
  • Get() – reads the data located in the array at the specified index, can be replaced with square brackets;
  • Delete() – deletes the element with the specified index;
  • Set() – replaces the data in the specified array cell.

One-dimensional and multidimensional arrays

In the simplest case, a one-dimensional array can contain values ​​of various types (Fig. 3)

Fig.3.

The result of executing the above code is shown in Fig. 4

Fig.4

Thus, we received a one-dimensional array consisting of a string value, a link to a directory element and a date. Moreover, when adding elements, we used two various methods Add() and Insert(), if we had used the Add() method when adding a date, our result would have had a slightly different look.

In high school, the concept of a two-dimensional matrix is ​​introduced. This is a set of data, each element of which has two indices (in the simplest case, a vertical and horizontal serial number); it is this that best illustrates the concept of a two-dimensional array.

When creating such a matrix, you can specify the number of rows and columns that will be used.

Let's say we are faced with the task of creating an array of the last name, first name and patronymic of two employees. Using the code (Fig. 5), we create a corresponding array with a fixed number of columns and rows.

Fig.5

To traverse it, we need two loops, in the first loop we traverse the array line by line, in the second we parse the line into elements. This can be done either using the cycle “For each .... From" (Fig. 6)

Fig.6

Or using a loop with the iterator “For ... by” (Fig. 7)

Fig.7

In principle, the number of dimensions of a particular array can be any, another thing is that in the case large quantity levels of detail, it is quite difficult to create a builder to read the data.

Using an Array

Arrays in 1C are most often used for:

  1. Creating various selections used when working with queries and other collections of values;
  2. Passing lists as parameters between procedures and functions;
  3. Exchange data with external components connected to using COM technologies.

Of course, this is far from a complete list of the purposes for which the “Array” object can be used.

An array in 1C is a set of some values. Values ​​in one array can be of different types.

The array can be obtained by executing various functions, for example, UnloadColumn() tables of values; you can get the selected rows as an array dynamic list etc. You can also create an array manually.

Creating an Array

1. How to create an array of the required size

pArray = new Array(4) ; //created an array of 4 elements

PMarray[ 0 ] = "We" ;
pArray[ 1 ] = "created" ;
pArray[ 2 ] = "new" ;
pArray[ 3 ] = "array" ;

2. How to create an empty array and add elements to it

pArray = new Array; //created an empty array

//Enter the values ​​of the array elements

PMassiv. Add("We");


3. How to create a multidimensional array.

Let's consider this issue using the example of a two-dimensional array, since arrays of large dimensions are used much less frequently, and the mechanism of their operation does not differ from a two-dimensional one.

pArray = new Array(4, 2); //created a 4x2 array

//Enter the values ​​of the array elements, numbering each word

PMarray[ 0 ] [ 0 ] = "1." ;
pArray[ 0 ] [ 1 ] = "We" ;
pArray[ 1 ] [ 0 ] = "2." ;
pArray[ 1 ] [ 1 ] = "created" ;
pArray[ 2 ] [ 0 ] = "3." ;
pArray[ 2 ] [ 1 ] = "multidimensional" ;
pArray[ 3 ] [ 0 ] = "4." ;
pArray[ 3 ] [ 1 ] = "array" ;

4. How to create a fixed array

A fixed array differs from a regular array in that it cannot be modified. You cannot add, delete, or change the values ​​of elements of such an array.

A fixed array can be obtained from a regular one:

pArray = new Array;

PMassiv. Add("We");
pArray. Add("created" ) ;
pArray. Add("new" ) ;
pArray. Add("array" ) ;

FArray = new FixedArray(pArray) ; // created a fixed array

Functions for working with arrays

We will consider the operation of functions using the example of a one-dimensional array pArray, created above and consisting of 4 elements:

  1. "created"
  2. "new"
  3. "array".
Function VBorder()

Gets the highest index of an array element. It is always one less than the number of array elements.

Index = pArray. BBorder() // 3;

Insert() function

Inserts a value into the array element at the specified index. Subsequent elements of the array are shifted

pArray. Insert(3, "new value") //Now the array consists of 5 elements

Function Add()

Creates a new element at the end of the array and inserts the given value there

pArray. Add("." ) // added a dot to the fifth element of the array;

Function Quantity()

Returns the number of array elements.

pArray. Quantity() ; // 4

Find() function

Searches an array for a given element. If found, returns its index. If not found, returns Undefined.

Index = pArray. Find("array" ) ; // 3
Index = pArray. Find( "line that didn't exist") ; // Undefined

Clear() function

Removes all values ​​from the array.

pArray. Clear() ;

Get() function

Gets the value of an array by index. The same problem can be solved using .

Value = pArray. Get(3) // "array"
Value= pArray[ 3 ]; // "array"

Delete() function

Removes an array element by index

pArray. Delete(3) ;

Function Set()

Sets the value of an array element by index. It works the same way.

pArray. Set(3, "array!");
pArray[ 3 ] = "array!" ;

How to traverse an array

You can iterate over all the elements of an array without specifying an index:

For each Array Element from pArray Loop
Report(ArrayElement) ;
EndCycle ;

You can use the index when traversing:

For Index= 0 by pArray. BBorder() Loop
Report(pArray[Index]);
EndCycle ;

How to traverse a multidimensional array

A multidimensional array is traversed using the same loops (see above), but one loop must be nested within another.

For each Element1 of mArray Cycle
For each Element2 of Element1 Cycle
Report(Element1) ;
EndCycle ;
EndCycle ;

Or using indexes.

mArray= new Array(3, 4);

For Index1 = 0 by mArray. BBorder() Loop
For Index2 by mArray[ Index1 ] . BBorder() Loop
Report(mArray[ Index1 ] [ Index2 ] ) ;
EndCycle ;
EndCycle ;

Sort an array

To sort the array, we need an auxiliary object of type ListValues.

ValueList = new ValueList; // create a list of values
ListValue. LoadValues(pArray) ; // load values ​​from the array into the list
ListValue. SortByValue(SortDirection.Age) ; //sort in ascending order
ListValue. SortByValue(SortDirection.Descending) ; //or descending
pArray = ListValue. Unload() ; // dump the sorted values ​​back into the array

Comparing two arrays

Before moving on to the description of the comparison function, let us agree that arrays are considered identical if they have the same number of elements and the corresponding elements of the arrays are equal. Then for comparison you can use the following function (by the way, such a function is already present in some standard configurations):

Function CompareArrays(Array1, Array2)

If Array1. Quantity()<>Array2. Quantity() Then
return FALSE; // Arrays are not equal, there is no point in comparing elements.
EndIf ;

For Index= 0 by Array1. BBorder() Loop
If Array1[Index]<>Array2[Index] Then
Return False ; //these elements are not equal, which means the arrays are not equal
EndIf ;
EndCycle ;

Return True ; // If you got here, then the arrays are equal
EndFunction

You need to pass 2 arrays to compare to the function. Function returns value True, if the arrays are equal, and Lie, if not equal.

The use of arrays in 1C helps to organize the elements used in work.

This is a software set of elements. It serves for:

  • Selecting values.
  • Sorting. Each position in the array is assigned an index. This makes the search easier. And you can refer to an object by writing its number.
  • Combining and saving records.
  • Separating groups, creating substrings.
  • An array is an object in itself. A variable will be attached to it. You can refer to individual parts of it or use it entirely in procedures. For example, you can combine the value group “Employees of Department N” and enter the first and last names of employees there.

Let's make an analogy. Imagine a bookcase. The books in it are arranged in different “cells”. If you keep records, you will know where each object is located. And you can get it without difficulty. But if books, magazines and newspapers are scattered chaotically on the table, the search for the right thing will take a long time. Although this does not reflect all the possibilities of unique collections.

Creation

Working with 1C arrays begins with their creation. You need a “location” - a field in which you will enter the code.

  1. Open "Configurator" ("Configuration").
  2. Click right click click on the “Processing” item.
  3. Select "Add".
  4. The Processing1 submenu appears. Select it. Information about it will be displayed in the work area.
  5. On the Basic tab, enter Name. Let's say "TestArray".
  6. Open the "Forms" section.
  7. Click on the magnifying glass icon.
  8. Check the “Processing form” option.
  9. Click on "Done".
  10. A window with three frames will open. In the upper right, go to the “Teams” tab.
  11. Plus icon to add a position.
  12. The “Team1” item will appear. Select it.
  13. In the "Name" field, enter a name. For example, “TestValues”. Copy it to "Header".
  14. In the “Action” line, click on the magnifying glass.
  15. Item “Create on client” and “OK”.
  16. Here is the module in which you can enter the code.

To display an intermediate result, create a “button”. Why it is needed in the array - you will understand a little later.

  1. Go to the "Forms" tab. She's downstairs.
  2. Right-click on "Command Bar".
  3. "Add - Button".
  4. Give it any name. Or you can leave the default one.
  5. In the “Team Name” field, click on the ellipsis icon.
  6. Select "TestValues". If the team was named differently, there will be a different heading.

Now you can start working with collections. But don’t think that in 1C you can immediately decompose a string into an array. It's better to start with something easier. Although all functions are a set of operators that you just need to remember.

  1. Open the “Module” tab.
  2. The text that comes after two forward slashes (two slashes - “//”) can be deleted. It looks something like this "//Insert handler contents". These are explanations. They are written by programmers so as not to get confused in the code.
  3. Write your text between the “Procedure” and “End of Procedure” operators.
  4. Now create a value set. Come up with a variable that will conditionally denote it. Let's say "FirstArray".
  5. It must be associated with an object. To do this, enter the command “FirstArray = New array()". This is a collection that can contain an unlimited number of elements. If you have a fixed size, specify it after the command in parentheses "New Array (5)". Accordingly, the two-dimensional collection will look like “New Array (7, 5)”. You can create multidimensional sets.
  6. As you type, suggestions may appear that suggest suitable operators.
  7. Words vary in color. Variables have one color, functions have another.
  8. After each command, place a semicolon ";". And for convenience, start with a new paragraph.
  9. You can't do anything with an empty set. Neither organize, nor output, nor decompose the string into an array in 1C. Therefore, we need to insert elements into it.
  10. Enter "FirstArray.Add(Value1);". Then “FirstArray.Add(Value2);” and so on.
  11. Objects are numbered from zero. That is, the variable “Value1” will be assigned the index “0”. You can fill out the collection yourself with “Value0” so as not to get confused.

What is all this for? And why that mysterious “button”? Let's try to run a simple function.

  1. Under the collection, write the command “Report (FirstArray);” The index of the element is indicated in square brackets. That is, the address of our “Value0”.
  2. Format the module. This is a check for errors. In large blocks, something can be left unfinished due to inattention: not adding a semicolon, not closing a parenthesis, or writing a variable incorrectly. The compiler will point this out.
  3. Click on the button in the form of a “Play” sign (a triangle in a circle) to run the program you wrote.
  4. Click on “Service” and select your button.
  5. Click it.
  6. The message “Value0” appears. This is the result of the function execution.

Shown most simple operation, so you understand how arrays generally work. With them you can create complex and multi-level programs. Display objects, sort, remove repetitions, split into a set of substrings, combine, split, search for information. You just need to know the operators.

Functions

All available functions are in the program reference book. Let's list a few to show the principle.

How to turn a 1C array into a string:

TestArray = New Array(4);
TestArray = This;
TestArray = Done;
TestArray = So;
RowTest = ValueInRowInt(TestArray);
Report(StringTest);

Output it to the “Button” and get the phrase. This may be part of a larger module - in which case the Report statement is not needed.

In order to decompose a string into an array of substrings in 1C, you need a separator. Conditional mark after which the text will be divided. Let's say there is the word "Protocol". And we will put the separator "O". Enter the command “TestArray = ExpandStringInArray(Protocol, “O”) ;” You will get “Pr”, “T”, “K” and “L” separately. The separator is erased. And the word is divided into several substrings.

Here's how to put it into practice. You have an array with the full names of employees. And you need the “Last Name”, “First Name” and “Patronymic” fields to be automatically filled in when selecting a position. To do this, the full name must be divided into three components. Use "space" as a separator.

Typically, a complex module is used for this: a loop with a large number of statements. Cyclic blocks with a condition are built according to the principle “If (condition), then (action1), otherwise (action2)”. First, the program checks whether the condition is true. If yes, then it runs the first function. If not, the second one.

The loop will check every character in the string. If there is no separator there, it continues further. If there is a delimiter there, it adds the value to the set of substrings. Other operators can be used there: for example, “AbcrLP” is an abbreviation of certain characters to the right and left of the line.

Each module must be “finished” so that the utility stops executing the procedure. On a loop with "If" this statement is "EndIf". On a cycle without conditions - “End of Cycle”. Each function must be completed separately.

Search and sort

Collections can contain a huge amount of data. We must learn to look for and sort them.

To run a search in an array in 1C, use the “Find” operator. You can also find the desired value visually. But if there are hundreds of elements, it is better to create an additional module and run a subroutine. Let's show it using a simple function as an example. But this is suitable for any blocks.

ArrayTest = New Array(3);
ArrayTest = "Phone";
ArrayTest = "Laptop";
ArrayTest = "Computer";
Index = ArrayTest.Find("Phone");
If Index ‹› Undefined Then
Report("Search completed" + ArrayTest[Index]);

And don't forget to put the "terminating statement".

The collection is provided as an example. We created a set of three values. And we did a short cycle. If the utility does not find a matching element, it will display the message “Undefined”. If he finds it, he will show the result.

Sorting a 1C array is simply irreplaceable when working with large amounts of data. You have to navigate them somehow. It is better to arrange them in a certain order. Let's say you have a collection called “YourArray”. To put it in order

TestList = New ValueList;
TestList.LoadValues(YourArray);
TestList.SortByValue(SortDirection.Age);
YourArray = TestList.UnloadValues();

Instead of “Ascending” you can put “Descending”. The result can also be uploaded to a table of values:

TestTable = New ValueTable;
TestTable.Columns.Add("Column");
TestTable.LoadColumn(YourArray, “Column”);
TestTable.Sort("Column Descending");
YourArray = TestTable.UnloadColumn("Column")

There are also complex sorting methods: “Bubble”, “Quick”, “Shaker”, “Recursion”. They have multi-level and long algorithms.

Tables and lists

Information can be transferred between different types of collections. To transfer an array to a table of values ​​in 1C:

ArrayTelephone = New Array;
ArrayPhone.Add("Smartphone");
ArrayPhone.Add("Tablet");
ArrayPhone.Add("Phone");
TableTest = New ValueTable;
TableTest.Columns.Add("Mobile phones");
TableTest.Add();
TableTest.Add();
TableTest.Add();
TableTest.LoadColumn(ArrayTelephone, “Mobile phones”);
For Each Row From TableTest Cycle
Report(String.Mobile);

Don't forget to add a loop termination statement at the end.

Converting an array into a list of values ​​in 1C is quite simple. You just need to upload data from one collection to another.

NewList = NewValueList();
NewList.LoadValues(YourArray);

In programming, one problem can be solved in different ways. And 1C is no exception. Linear algorithms, loops, conditional loops, counter loops. There are many options. To navigate the array, you need some 1C skills.

© 2024 ermake.ru -- About PC repair - Information portal