Jump to content
Macro Express Forums

Array length


Recommended Posts

The documentation states:

Get Array Length returns the maximum number of elements defined for an array variable. This command is useful when used in conjunction with a Repeat command. Use it to change the number of array elements without modifying the macro script.

Can anyone provide an example of changing the number of elements without modifying the macro script please? This sentence seems to imply that the number of elements in an array can be changed at runtime. How?

Link to comment
Share on other sites

The wording is certainly ambiguous. I think it means, you can write a Repeat loop to process an array, with number of repetitions specified in an integer variable like N[1]. So instead of coding REPEAT 100 TIMES you would code REPEAT N[1] TIMES. In your script, just before the REPEAT command you would get the number of elements into N[1].

 

Then, at a later date, if you redefine the array to allow for fewer or more elements, you don't have to change the processing logic, because you did not hard-code the number into the REPEAT command.

Link to comment
Share on other sites

The wording is certainly ambiguous. I think it means, you can write a Repeat loop to process an array, with number of repetitions specified in an integer variable like N[1]. So instead of coding REPEAT 100 TIMES you would code REPEAT N[1] TIMES. In your script, just before the REPEAT command you would get the number of elements into N[1].

 

Then, at a later date, if you redefine the array to allow for fewer or more elements, you don't have to change the processing logic, because you did not hard-code the number into the REPEAT command.

I think you're right.

Quite honestly, I can see absolutely no point in the Get Array Length command. Since arrays have to be declared at design time together with the number of elements, what does Get Array Length do that is at all useful? I'd have hoped that the Split command could dynamically assign the number of elements it needs, but, sadly, not.

What am I missing?

Link to comment
Share on other sites

One thing I would use it for is to preset Array values using a Repeat loop. I think I mentioned in a previous post that there is no way to tell if Array members have actually been set. If you find a "" or 0 or 0.0 you may not know whether it's a genuine value or the default. I would use a repeat loop to set to say, "notset" or some integer or decimal value that is unlikely to be met. That way you can evaluate empty from filled array members. Not necessary for most purposes but useful for critical data and very large databases that you cannot possibly know from memory.

 

(edit) Come to think of it that also comes under Paul's comment about declaring size at time of creation. I suppose if you have a number of arrays within a macro that you are processing it could be useful.

Edited by JohnS
Link to comment
Share on other sites

Quite honestly, I can see absolutely no point in the Get Array Length command. Since arrays have to be declared at design time together with the number of elements, what does Get Array Length do that is at all useful? I'd have hoped that the Split command could dynamically assign the number of elements it needs, but, sadly, not.

What am I missing?

I have written macros that use an array of a specific length (say 25) and later had to increase the length of the array (to 50 or 100 elements). Because I used the Get Array Length command all I needed to do was change the number of elements in the array variable under the 'Variables' tab.

Link to comment
Share on other sites

What is the penalty, in memory or otherwise, of creating a string array much larger than you ever would need? Any logic to process elements sequentially could be written simply to stop when an empty element is reached. Does the array take up significant space if values have not yet been assigned to the elements?

Link to comment
Share on other sites

What is the penalty, in memory or otherwise, of creating a string array much larger than you ever would need? Any logic to process elements sequentially could be written simply to stop when an empty element is reached. Does the array take up significant space if values have not yet been assigned to the elements?

Larger variable arrays do take up more space. My educated guess is that each element in the array takes around 16 bytes. I haven't really thought about how much space they take, just that it takes some space.

Link to comment
Share on other sites

It depends.

..................................

Normally I use the logic "If is null" to encapsulate a break and that works fine. But in a few cases I've had arrays where there could be holes in the sequence. IOW it might be perfectly valid to have noting in element 56. Usually these are a part several arrays used to form a table. However I usually count the ones I create or count the elements when propagated and only repeat the necessary number of times. I do not think I have ever used the Get Array Length. However I do have a routine that starts from the highest element and counts down until it finds one that is not null I use when my data comes from somewhere else.

Link to comment
Share on other sites

I have written macros that use an array of a specific length (say 25) and later had to increase the length of the array (to 50 or 100 elements). Because I used the Get Array Length command all I needed to do was change the number of elements in the array variable under the 'Variables' tab.

I don't follow. DId you change the number of array elements at run time or design time? If at design time, what advantage did Get Array Length offer other than avoid having to look at the Variables tab to see the array length?

Link to comment
Share on other sites

I don't follow. DId you change the number of array elements at run time or design time? If at design time, what advantage did Get Array Length offer other than avoid having to look at the Variables tab to see the array length?

Two examples.

 

I have repeat loops that loop through a variable array. If I set the repeat command to loop 25 times, I have to go back and edit that command when the array is increased to 100. If you are only using one loop then this might not be very difficult as long as you remember to make the change. But some macros are long and complex and it is time consuming to track down all commands that have hard coded numbers.

 

In another type of macro an array is used to process information but the number of elements in the input source (a .csv file, a list of files in a folder, etc.) might vary. The macro works fine for days, weeks or even months but one day stops working with an error message. Now time must be spent figuring out why the macro no longer works. (I may not remember how the macro is written and I have to study it to figure it out again or it may have been written by someone else.) Initially 100 elements in the array were enough but now it needs 101.

 

A few lines of macro commands can check to make sure that the index the macro is about to use does not exceed the number of elements in the array variable. If there is a problem, a warning message is displayed and the macro halts.

 

By using Get Array Length the length of the array can be changed and the macro can be fixed without editing the script commands in the macro. You change the length in one spot and don't have to change it throughout the macro.

 

This is the same concept of using variables to hold delay times, file names and/or paths and setting these variables at the top of a macro.

Link to comment
Share on other sites

Simple. In the IF dialog box simply use the condition "Equals" and leave the comparison value blank. Before you do anything with a variable in MEP the value is null (blank).

 

Now bear in mind when I say null I mean null as a mathematician or engineer would mean it. That is nothing or blank. That is not the same as zero. Zero is a quantitative value. But in computer parlance null is a character. In ASCII it is 0x00 or 00000000. If I remember my CS classes from the 80's some computer systems can not deal with a true blank (zero length) value in memory so it's set to the null character. But in logic it functions just like a logical or mathematical null. To me I like to think of it like the aircraft maintenance manuals I used to use. Inevitably there was a page within that was completely blank except for the sentence "This page intentionally left blank". Of course the philosopher in me saw this as a perfect oxymoron. Anyway I like to think that's how the computers regard the null character.

  • Like 1
Link to comment
Share on other sites

With simple T, N, D variables you are generally dealing with small amounts of data. I know that you could still make arrays back in the old days but it needed some expertese. Now that it's so much easier for the novice to blindly gather large amounts of data, it is more difficult to decide if values have actually been set. I would imagine it could be really bad in companies collecting data from all over the place where no one person can see what is valid and what is not. If it's "# of Toyotas sold", 0 could be valid or the salesman was too lazy to fill the field.

 

The problem is the same for the T, N, D variables of old so it's nothing new. Taking an integer array:

 

%myvalue[1]% is set to 1

%myvalue[2]% is not set

%myvalue[3]% is set to 0

 

If you use in a loop for n

Get %myvalue[n]%

You will get the same result for %myvalue[2]% and %myvalue[3]%, 0, because the default is 0. Read any unset integer array member and it's 0. You cannot use "" with Integers, which is not surprising; you have to compare to 0.

 

In your Text String example, you could not tell if it was a genuine blank entry, "Your thoughts on Relativity", or the default blank setting.

 

To have a flag to indicate "Set" would need 1 bit per array member in ME, although probably more than that in practice. If you were processing the array, all unset members would be skipped to save time. It won't cure incorrect data entry but at least tackles the initial filling of the array. If you don't fill in the required fields in web forms, it tells you fairly quickly.

Link to comment
Share on other sites

Simple. In the IF dialog box simply use the condition "Equals" and leave the comparison value blank. Before you do anything with a variable in MEP the value is null (blank).

 

Now bear in mind when I say null I mean null as a mathematician or engineer would mean it. That is nothing or blank. That is not the same as zero. Zero is a quantitative value. But in computer parlance null is a character. In ASCII it is 0x00 or 00000000. If I remember my CS classes from the 80's some computer systems can not deal with a true blank (zero length) value in memory so it's set to the null character. But in logic it functions just like a logical or mathematical null. To me I like to think of it like the aircraft maintenance manuals I used to use. Inevitably there was a page within that was completely blank except for the sentence "This page intentionally left blank". Of course the philosopher in me saw this as a perfect oxymoron. Anyway I like to think that's how the computers regard the null character.

AFAIK, there is no concept of nulls in MEP. An integer variable equals 0 before it is used for the first time. A text variable equals a 0-length string, i.e. "", before it is used for the first time.

I do not believe you can distinguish between the values of %t[1]% before it is ever used, and Variable Set String %t[1]% to "". In which case, that's not a Null value in any sense of the word, neither programming nor engineering.

 

If the value of a variable is unknown, then you cannot compare it with anything, neither blank nor an empty string. Thus, in SQL, you cannot compare 2 null variables with one another. All you can do is test the variable to see if its value is unknown, i.e. IF MyVar IS NULL, not IF MyVar = NULL.

 

Your earlier example of the blank page is just that: a blank page, and not a page with an unknown value.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...