Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Named array keys

Reply
 
Old 09-03-2009   #1 (permalink)
Turgs


 
 

Named array keys

Hello

I'm fairly new to Vbscript and I'm struggling a little as I'm more
familiar with Javascript.
Can someone please give me a hand?

Does vbscript allow the use of named array keys?

For example I have:

Dim result(3)
result(age) = 60
result(firstname) = "Tony"
result(height) = 187
result(weight) = 76

msgbox("Age: " & result(age) & vbCr &_
"First Name: " & result(firstname) & vbCr &_
"Height: " & result(height) & vbCr &_
"Weight: " & result(weight))

The resulting msgbox shows:

Age: 76
Name: 76
Height: 76
Weight: 76

This seems to assign *every* element in the result array to 76, which
should only be assigned to the "weight" element.

Is this happening because vbscript only accepts integers as the key/
index for arrays?

Any help is greatly appreciated.

Thanks
Turgs

My System SpecsSystem Spec
Old 09-03-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Named array keys


"Turgs" <timb.php@xxxxxx> wrote in message
news:a10b262d-5b05-4833-830f-a0de88f4619d@xxxxxx
Quote:

> Hello
>
> I'm fairly new to Vbscript and I'm struggling a little as I'm more
> familiar with Javascript.
> Can someone please give me a hand?
>
> Does vbscript allow the use of named array keys?
>
> For example I have:
>
> Dim result(3)
> result(age) = 60
> result(firstname) = "Tony"
> result(height) = 187
> result(weight) = 76
>
> msgbox("Age: " & result(age) & vbCr &_
> "First Name: " & result(firstname) & vbCr &_
> "Height: " & result(height) & vbCr &_
> "Weight: " & result(weight))
>
> The resulting msgbox shows:
>
> Age: 76
> Name: 76
> Height: 76
> Weight: 76
>
> This seems to assign *every* element in the result array to 76, which
> should only be assigned to the "weight" element.
>
> Is this happening because vbscript only accepts integers as the key/
> index for arrays?
>
You are correct. Since you have not assigned values to Age, Name, Height and
Weight, they have assumed a value of 0. You are therefore accessing the
first element of your array in all your operations.


My System SpecsSystem Spec
Old 09-03-2009   #3 (permalink)
Paul Randall


 
 

Re: Named array keys


"Turgs" <timb.php@xxxxxx> wrote in message
news:a10b262d-5b05-4833-830f-a0de88f4619d@xxxxxx
Quote:

> Hello
>
> I'm fairly new to Vbscript and I'm struggling a little as I'm more
> familiar with Javascript.
> Can someone please give me a hand?
>
> Does vbscript allow the use of named array keys?
>
> For example I have:
>
> Dim result(3)
> result(age) = 60
> result(firstname) = "Tony"
> result(height) = 187
> result(weight) = 76
>
> msgbox("Age: " & result(age) & vbCr &_
> "First Name: " & result(firstname) & vbCr &_
> "Height: " & result(height) & vbCr &_
> "Weight: " & result(weight))
>
> The resulting msgbox shows:
>
> Age: 76
> Name: 76
> Height: 76
> Weight: 76
>
> This seems to assign *every* element in the result array to 76, which
> should only be assigned to the "weight" element.
>
> Is this happening because vbscript only accepts integers as the key/
> index for arrays?
>
> Any help is greatly appreciated.
>
> Thanks
> Turgs
"named array keys" sounds a lot like a term that might be used with
associative arrays, which in JScript/VBScript is implemented as a dictionary
object.

Is the following script more like what you want to do?
'Dim result(3)
Dim result
Set result = CreateObject("scripting.dictionary")

result("age") = 60
result("firstname") = "Tony"
result("height") = 187
result("weight") = 76

msgbox("Age: " & result("age") & vbCr &_
"First Name: " & result("firstname") & vbCr &_
"Height: " & result("height") & vbCr &_
"Weight: " & result("weight"))

-Paul Randall


My System SpecsSystem Spec
Old 09-03-2009   #4 (permalink)
Turgs


 
 

Re: Named array keys

On Sep 3, 10:45*pm, "Paul Randall" <Paulr...@xxxxxx> wrote:
Quote:

> "Turgs" <timb....@xxxxxx> wrote in message
>
> news:a10b262d-5b05-4833-830f-a0de88f4619d@xxxxxx
>
>
>
Quote:

> > Hello
>
Quote:

> > I'm fairly new to Vbscript and I'm struggling a little as I'm more
> > familiar with Javascript.
> > Can someone please give me a hand?
>
Quote:

> > Does vbscript allow the use of named array keys?
>
Quote:

> > For example I have:
>
Quote:

> > * * *Dim result(3)
> > * * *result(age) = 60
> > * * *result(firstname) = "Tony"
> > * * *result(height) = 187
> > * * *result(weight) = 76
>
Quote:

> > * * *msgbox("Age: " & result(age) & vbCr &_
> > * * * * * * "First Name: " & result(firstname) & vbCr &_
> > * * * * * * "Height: " & result(height) & vbCr &_
> > * * * * * * "Weight: " & result(weight))
>
Quote:

> > The resulting msgbox shows:
>
Quote:

> > * * *Age: 76
> > * * *Name: 76
> > * * *Height: 76
> > * * *Weight: 76
>
Quote:

> > This seems to assign *every* element in the result array to 76, which
> > should only be assigned to the "weight" element.
>
Quote:

> > Is this happening because vbscript only accepts integers as the key/
> > index for arrays?
>
Quote:

> > Any help is greatly appreciated.
>
Quote:

> > Thanks
> > Turgs
>
> "named array keys" sounds a lot like a term that might be used with
> associative arrays, which in JScript/VBScript is implemented as a dictionary
> object.
>
> Is the following script more like what you want to do?
> 'Dim result(3)
> Dim result
> Set result = CreateObject("scripting.dictionary")
>
> result("age") = 60
> result("firstname") = "Tony"
> result("height") = 187
> result("weight") = 76
>
> msgbox("Age: " & result("age") & vbCr &_
> *"First Name: " & result("firstname") & vbCr &_
> *"Height: " & result("height") & vbCr &_
> *"Weight: " & result("weight"))
>
> -Paul Randall
Hi Paul

Yes, that's exactly what I want to do - "associative arrays".

Thanks
Tim
My System SpecsSystem Spec
Old 09-03-2009   #5 (permalink)
Richard Mueller [MVP]


 
 

Re: Named array keys


"Turgs" <timb.php@xxxxxx> wrote in message
news:af99aba1-67bd-4055-abfe-1576916c4446@xxxxxx
On Sep 3, 10:45 pm, "Paul Randall" <Paulr...@xxxxxx> wrote:
Quote:

> "Turgs" <timb....@xxxxxx> wrote in message
>
> news:a10b262d-5b05-4833-830f-a0de88f4619d@xxxxxx
>
>
>
Quote:

> > Hello
>
Quote:

> > I'm fairly new to Vbscript and I'm struggling a little as I'm more
> > familiar with Javascript.
> > Can someone please give me a hand?
>
Quote:

> > Does vbscript allow the use of named array keys?
>
Quote:

> > For example I have:
>
Quote:

> > Dim result(3)
> > result(age) = 60
> > result(firstname) = "Tony"
> > result(height) = 187
> > result(weight) = 76
>
Quote:

> > msgbox("Age: " & result(age) & vbCr &_
> > "First Name: " & result(firstname) & vbCr &_
> > "Height: " & result(height) & vbCr &_
> > "Weight: " & result(weight))
>
Quote:

> > The resulting msgbox shows:
>
Quote:

> > Age: 76
> > Name: 76
> > Height: 76
> > Weight: 76
>
Quote:

> > This seems to assign *every* element in the result array to 76, which
> > should only be assigned to the "weight" element.
>
Quote:

> > Is this happening because vbscript only accepts integers as the key/
> > index for arrays?
>
Quote:

> > Any help is greatly appreciated.
>
Quote:

> > Thanks
> > Turgs
>
> "named array keys" sounds a lot like a term that might be used with
> associative arrays, which in JScript/VBScript is implemented as a
> dictionary
> object.
>
> Is the following script more like what you want to do?
> 'Dim result(3)
> Dim result
> Set result = CreateObject("scripting.dictionary")
>
> result("age") = 60
> result("firstname") = "Tony"
> result("height") = 187
> result("weight") = 76
>
> msgbox("Age: " & result("age") & vbCr &_
> "First Name: " & result("firstname") & vbCr &_
> "Height: " & result("height") & vbCr &_
> "Weight: " & result("weight"))
>
> -Paul Randall
Hi Paul

Yes, that's exactly what I want to do - "associative arrays".

Thanks
Tim

-------------
For more on the Dictionary object, check this link:

http://www.microsoft.com/technet/scr..._scr_ildk.mspx

Use the TOC at the left to navigate to topics on creating the object and
manipulating keys and items.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 09-03-2009   #6 (permalink)
Bob Barrows


 
 

Re: Named array keys

Paul Randall wrote:
Quote:

> "named array keys" sounds a lot like a term that might be used with
> associative arrays, which in JScript/VBScript is implemented as a
> dictionary object.
Minor correction here. What you say is certainly true with vbscript, but
in jscript objects can be used as associative arrays out of the box,
without resorting to a dictionary object:
http://www.quirksmode.org/js/associative.html
http://blog.xkoder.com/2008/07/10/ja...s-demystified/
--
HTH,
Bob Barrows


My System SpecsSystem Spec
Old 09-03-2009   #7 (permalink)
Paul Randall


 
 

Re: Named array keys


"Bob Barrows" <reb01501@xxxxxx> wrote in message
news:ei0vrRKLKHA.4316@xxxxxx
Quote:

> Paul Randall wrote:
Quote:

>> "named array keys" sounds a lot like a term that might be used with
>> associative arrays, which in JScript/VBScript is implemented as a
>> dictionary object.
>
> Minor correction here. What you say is certainly true with vbscript, but
> in jscript objects can be used as associative arrays out of the box,
> without resorting to a dictionary object:
> http://www.quirksmode.org/js/associative.html
> http://blog.xkoder.com/2008/07/10/ja...s-demystified/
> --
> HTH,
> Bob Barrows
Thanks for the correction. My JScript knowledge is pretty much limited to
what I find in the script56.chm file, like:
mk:@MSITStore:C:\SCRIPT56.CHM::/html/jsobjdictionary.htm

-Paul Randall


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Fast copy method of sub array (=array range) possible? VB Script
How to create array without quotes? $array = (a,b,c) PowerShell
Stupid Array Tricks: Initializing an Array to a Certain Size PowerShell
keys & SHIFT keys not working properly in Vista Home Premium Vista General
how to assign values to array and how to create array via variable PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46