VBScript: Array Functions - UBound and LBound
(Page 5 of 6 )
If you need to know the size of an array, then you need the UBound and LBound functions. The LBound returns the smallest index number, which of course will always be 0. The UBound returns the largest index number. Note that while UBound might return the highest index number, there may not be a value in that index. Here they are at work:
<html>
<body>
<script type="text/vbscript">
dim super(5)
super(0)="I"
super(1)="Am"
super(2)="Iron"
super(3)="Man"
document.write(UBound(super))
document.write("<br />")
document.write(LBound(super))
</script>
</body>
</html>
This gives the result:
5
0
Filtering Function
You can use the Filter function to filter the items in an array. Let's say you have a list of some He-Man heroes and villains and you want to see all of the ones with the word "man" in their name (there are more than you think):
<html>
<body>
<script type="text/vbscript">
dim grayskull(5),a
grayskull(0)="He-Man"
grayskull(1)="Man at Arms"
grayskull(2)="Skeletor"
grayskull(3)="Evil Lynn"
grayskull(4)="Stinkor"
a=Filter(grayskull,"Man")
document.write(a(0) & "<br />")
document.write(a(1) & "<br />")
document.write(a(2) & "<br />")
document.write(a(3) & "<br />")
document.write(a(4))
</script>
</body>
</html>
This returns the following names:
He-Man
Man at Arms
Now if you are looking for the elements that DO NOT contain "man," you simply add false to the equation, like this:
<html>
<body>
<script type="text/vbscript">
dim grayskull(5),a
grayskull(0)="He-Man"
grayskull(1)="Man at Arms"
grayskull(2)="Skeletor"
grayskull(3)="Evil Lynn"
grayskull(4)="Stinkor"
a=Filter(grayskull,"Man",false)
document.write(a(0) & "<br />")
document.write(a(1) & "<br />")
document.write(a(2) & "<br />")
document.write(a(3) & "<br />")
document.write(a(4))
</script>
</body>
</html>
This results in:
Skeletor
Evil Lynn
Stinkor
Next: The Join Function >>
More Windows Scripting Articles
More By James Payne