![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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? > Weight, they have assumed a value of 0. You are therefore accessing the first element of your array in all your operations. |
My System Specs![]() |
| | #3 (permalink) |
| | 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 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 Specs![]() |
| | #4 (permalink) |
| | 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 Yes, that's exactly what I want to do - "associative arrays". Thanks Tim |
My System Specs![]() |
| | #5 (permalink) |
| | 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 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 Specs![]() |
| | #6 (permalink) |
| | 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. 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 Specs![]() |
| | #7 (permalink) |
| | 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 what I find in the script56.chm file, like: mk:@MSITStore:C:\SCRIPT56.CHM::/html/jsobjdictionary.htm -Paul Randall |
My System Specs![]() |
![]() |
| 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 | |||