Difference between revisions of "IndexLength"

(Introduced in Analytica 5.0)
 
 
(2 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
== IndexLength(I) ==
 
== IndexLength(I) ==
  
Returns the number of elements in Index «<code>I</code>».
+
Returns the number of elements in index «I».
  
[[IndexLength]] is closely related to the <code>[[Size]](a)</code> function, but differs in that <code>[[IndexLength]](I)</code> gives the size of the ''index'' <code>I</code>, whereas <code>[[Size]](a)</code> gives the size of the ''value'' <code>a</code>. For a simple, non-dynamic index, these are the same, but they are usually different in the case where <code>a</code> is a multi-dimensional [[Self-Indexed Arrays|self-indexed array]], or where the size of a dynamic index is requested from within a [[dynamic loop]].
+
[[IndexLength]] is closely related to the [[Size]] function, but differs in that <code>IndexLength(I)</code> gives the size of the ''index'' <code>I</code>, whereas <code>Size(a)</code> gives the size of the ''value'' <code>a</code>. For a simple, non-dynamic index, these are the same, but they are usually different in the case where <code>a</code> is a multi-dimensional [[Self-Indexed Arrays|self-indexed array]], or where the size of a dynamic index is requested from within a [[dynamic loop]].
  
<code>[[IndexLength]](I)</code> is equivalent to <code>[[Size]]([[IndexValue]](I))</code>, and to <code>[[Sum]](1,I)</code>.
+
<code>IndexLength(I)</code> is equivalent to <code>Size(IndexValue(I))</code>, and to <code>Sum(1, I)</code>.
 +
 
 +
== Explanatory Video ==
 +
 
 +
<center>
 +
<iframe width="640" height="360" src="https://www.youtube.com/embed/rKR_YGitMPo" frameborder="0" allowfullscreen></iframe>
 +
 
 +
'''Video:''' [https://youtu.be/rKR_YGitMPo Why the IndexLength function is probably better than alternatives] (4 minutes)
 +
</center>
  
 
== Examples ==
 
== Examples ==
  
:<code>Index The_Date := Sequence( 20-Jan-2009, 19-Jan-2017) Do IndexLength(The_Date)</code> &rarr; 2922
+
:<code>Index The_Date := Sequence(20-Jan-2009, 19-Jan-2017) Do IndexLength(The_Date) &rarr; 2922</code>
  
 
Consider the case where variable <code>A</code> is a [[Self-Indexed Arrays|self-indexed array]]:
 
Consider the case where variable <code>A</code> is a [[Self-Indexed Arrays|self-indexed array]]:
:Index Digit := <code>0..9</code>
+
<pre style="background:white; border:white; margin-left: 1em;">
:Variable A := [0,10,20,30] + Digit
+
Index Digit := 0..9
:<code>[[Size]](A)</code> &rarr; 40
+
Variable A := [0, 10, 20, 30] + Digit
:<code>[[IndexLength]](A)</code> &rarr; 4
+
Size(A) &rarr; 40
 +
IndexLength(A) &rarr; 4
 +
</pre>
  
 
Suppose you need the length of the [[Time]] index from within a [[dynamic loop]]:
 
Suppose you need the length of the [[Time]] index from within a [[dynamic loop]]:
:Index Time := <code>2017..2025</code>
+
<pre style="background:white; border:white; margin-left: 1em;">
:<code>[[Dynamic]]( [[Size]](Time) )</code> &rarr; Array(Time, [1,1,1,1,1,1,1,1,1] )
+
Index Time := 2017..2025
:<code>[[Dynamic]]( [[IndexLength]](Time) )</code> &rarr; Array(Time, [9,9,9,9,9,9,9,9,9])
+
Dynamic(Size(Time)) &rarr; Array(Time, [1, 1, 1, 1, 1, 1, 1, 1, 1])
Inside a [[dynamic loop]], the ''value'' of [[Time] is the single time point, hence its [[Size]] is always 1. Hence, you should use [[IndexLength]] when you really want the length of the [[Time]] index.
+
Dynamic(IndexLength(Time)) &rarr; Array(Time, [9, 9, 9, 9, 9, 9, 9, 9, 9])
 +
</pre>
 +
 
 +
Inside a [[dynamic loop]], the ''value'' of [[Time]] is the single time point, hence its [[Size]] is always 1. Hence, you should use [[IndexLength]] when you really want the length of the [[Time]] index.
  
 
== History ==
 
== History ==
  
[[IndexLength]] was introduced as a built-in function in [[Analytica 5.0]]. Prior to [[Analytica 5.0]], you can define your own [[User-Defined Function]]:
+
[[IndexLength]] was introduced as a built-in function in [[Analytica 5.0]]. Prior to [[Analytica 5.0]], you can define your own [[User-Defined Function]] by:
  
:'''Function''' <code>IndexLength( I : Index ) := [[Size]](I)</code>
+
:<code>Function IndexLength(I : Index) := Size(I)</code>
  
or use the more verbose expression <code>[[Size]]([[IndexValue]](I))</code> when you need the size of the index.
+
or use the more verbose expression <code>Size(IndexValue(I))</code> when you need the size of the index.
  
 
== See Also ==  
 
== See Also ==  
 +
* [http://lumina.com/blog/analytica-5.0-video-short-the-length-of-an-index Video Short: The length of an index]
 +
* [[Index]]
 +
* [[Index..Do]]
 +
* [[IndexValue]]
 +
* [[Array]]
 
* [[Size]]
 
* [[Size]]
* [[IndexValue]]
+
* [[Dynamic]]

Latest revision as of 01:17, 9 March 2018


new to Analytica 5.0

IndexLength(I)

Returns the number of elements in index «I».

IndexLength is closely related to the Size function, but differs in that IndexLength(I) gives the size of the index I, whereas Size(a) gives the size of the value a. For a simple, non-dynamic index, these are the same, but they are usually different in the case where a is a multi-dimensional self-indexed array, or where the size of a dynamic index is requested from within a dynamic loop.

IndexLength(I) is equivalent to Size(IndexValue(I)), and to Sum(1, I).

Explanatory Video

Video: Why the IndexLength function is probably better than alternatives (4 minutes)

Examples

Index The_Date := Sequence(20-Jan-2009, 19-Jan-2017) Do IndexLength(The_Date) → 2922

Consider the case where variable A is a self-indexed array:

Index Digit := 0..9
Variable A := [0, 10, 20, 30] + Digit
Size(A) → 40
IndexLength(A) → 4

Suppose you need the length of the Time index from within a dynamic loop:

Index Time := 2017..2025
Dynamic(Size(Time)) → Array(Time, [1, 1, 1, 1, 1, 1, 1, 1, 1])
Dynamic(IndexLength(Time)) → Array(Time, [9, 9, 9, 9, 9, 9, 9, 9, 9])

Inside a dynamic loop, the value of Time is the single time point, hence its Size is always 1. Hence, you should use IndexLength when you really want the length of the Time index.

History

IndexLength was introduced as a built-in function in Analytica 5.0. Prior to Analytica 5.0, you can define your own User-Defined Function by:

Function IndexLength(I : Index) := Size(I)

or use the more verbose expression Size(IndexValue(I)) when you need the size of the index.

See Also

Comments


You are not allowed to post comments.