Friday, July 11, 2008

Handling Arrays in VBScript

An array is a collection of logically related data items, where
each item can either be accessed in a sequential order, or
through supplying its index position.

Consider this:

<script language="VBScript">

dim names(5)

names(0)="Rohit" names(1)="Prakash" names(2)="Asha"
names(3)="Tom" names(4)="Lata" names(5)="Kailash"

for i=0 to 5 document.write names(i) & "
" next

</script>

Here we declare an index of dimension 5, which later on, comes
to store six names, as, an index in an array starts at 0.

Then the for loops dumps the entire array onto the HTML page.
People familiar with the for() {.} loop of JavaScript will
notice that the for.next loop of VBScript is less cryptic.

Since now we have learnt how to create a VBScript array, let us
try to sort this array. We'll sort the array in ascending order,
that is, from the lowest to the top most in the alphabetical
array.

So what's the logic?

1. Compare the first name with the second name 2. If the second
name is lower than the first name, then swap 3. else 4. Compare
the first name with the third name 5. If the third name is lower
than the first name, then swap 6. else 7. Compare the first name
with the fourth name 8. If the fourth name is lower than the
first name, then swap 9. else 10. Compare the first name with
the fifth name 11. If the fifth name is lower than the first
name, then swap

So by the time we have reached the 11th step, we have the lowest
name at the first position.

The second round starts at the second index index, as the first
we already know is the lowest.

1. Compare the second name with the third name 2. If the third
name is lower than the second name, then swap 3. else 4. Compare
the second name with the fourth name

and so on

I hope you've understood the logic now. Let's see how it looks
programmatically:

<script language="VBScript">

dim names(5) names(0)="Rohit" names(1)="Prakash" names(2)="Asha"
names(3)="Tom" names(4)="Lata" names(5)="Kailash"

for i=0 to 4 for j=i+1 to 5 if names(i)>names(j) then
temp=names(j) names(j)=names(i) names(i)=temp end if next next

for i=0 to 5 document.write names(i) & "
" next

</script>

There is a nested loop here. Study the flow of the for i=0 to 4
loop. The outer loop holds the index of the item that will be
compared with every other, next item. For instance, when the
first cycle of the loop begins, i is zero, and when the program
ends the nested loop, j starts at i+1, that is, 1. So turn by
turn, item at index zero is compared with every item in the
array. The outer array goes only till 4 because when the nested
loop begins, the second last item will be compared with the last
item: j=i+1

Save the file, and check it out in your browser.

Now we see the use of a dynamically defined array, and how that
array can be filled using the inputbox() function.

<script language="VBScript">

dim strings() i=0 redim strings(i)

strings(i)=""

do

holdstring=inputbox("Please enter a string, 'fin' to stop.")

if holdstring<>"fin" then

strings(i)=holdstring i=i+1 redim preserve strings(i)

else

exit do

end if

loop

if strings(0)<>"" then

for j=0 to i

document.write strings(j) & "
"

next

end if

</script>

strings is an array that is re-dimensioned whenever the user
types in a new string. When we declare an array, we reserve the
memory space for that array, that is, if we say

dim arr(20)

it means we are reserving the space for twenty data items. But
what if the user enters just five items? The rest of the space
is wasted. To avoid this, we declare a dynamic array.

In the above script, we have a loop whose continuance depends on
the user input. It keeps on loop until the user enters "fin".
So, when "fin" is not entered, only then the dimension of the
array strings is increased.

When we are re-dimensioning an array, we have to use the keyword
preserve to preserve the existing content of the array. In its
absence, the array gets re-dimensioned, but the values are lost.

Exit do forces the program to come out of the loop if the
condition that terminates it, occurs.


Amrit Hallan is a freelance web designer.

Tips For the Late Blooming Computer User

For years I've been a stay-at-home mom, and I have only recently
gone back into the work force. I've done pretty well for myself,
but I could have really saved some time and frustration in the
beginning if I'd just known a few things about how to use a
computer.

I come from the era of the typewriter and adding machine, and
the computer was really intimidating to me at first. When I
first entered the business world, I lived in constant fear of
losing my document or crashing my machine. The word processor
was my enemy, as was business email. The only thing I could
figure out was how to open incoming messages. This was fine for
trading those cute stories about angels with my friends, but it
had my boss in a rage. Now, those of you who know the difference
between an attachment and a hyperlink can stop reading, but if
you're as confused as I once was, read on. I've got some useful
tips.

First, face your fear. The computer is just a machine. It's not
going to yell at you or blow up. Don't be afraid to press a key
or click on an icon (those little picture boxes that link you to
programs and things.) Quite often I would end up staring at my
screen afraid to move, because I didn't know what to do next and
I was afraid of the consequences of making the wrong decision. I
wasted so much time. Just dive in. If you end up doing something
that looks funny or wrong, you can almost always find a solution.

Second, save your work. This is so important in word processing
programs. Have you ever been typing along, only to hit a wrong
key and have your entire document disappear? Boy, I sure have.
This doesn't have to happen. If you just remember to hit "save"
(under the file menu on most all word processors) every few
sentences, the most you'll ever lose is a phrase or two.

Third, get to know your undo and help keys. "Undo" simply
reverses the last action completed on the computer. If you hit
something that makes your document go all kerflooey, chances are
you can just click "undo" (under the edit menu usually) a time
or two and you'll be fine. The "help" menu is another great
asset in both word processors and email programs. Rather than
sitting there wondering how many times you've already asked your
boss to explain something, just go to "help" and then type in a
question like "how do I set up columns, or "how do I attach a
file," and the help feature will walk you right through the
steps. It can take you a little time, but it's better than
constantly running to your boss and looking helpless.

Fourth, make a computer savvy friend. This is so important. If
there's someone in the office who seems to be really good at the
computer, bring them some cookies or take them to lunch and ask
them if you can have a little of their time. Have them sit with
you for a few minutes while you work on a document or email. Do
this on a few different occasions. Chances are they'll not only
be able to answer your questions, but they'll be able to point
out some great shortcuts for you, too.

Computers don't have to be the unconquerable beasts they
sometimes appear to be. Just face your fear and approach the
machine calmly and logically, and chances are you'll be doing
fine. The better you are at using your computer, the better
you'll be at your job, and the happier people will be with you!

Author: Lisa Lake