Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > alt.comp.lang.vbscript > #22 > unrolled thread

VBScript dictionary delete by index ?

Started by"R.Wieser" <address@is.invalid>
First post2025-11-06 10:03 +0100
Last post2025-11-07 09:15 +0100
Articles 10 — 3 participants

Back to article view | Back to alt.comp.lang.vbscript


Contents

  VBScript dictionary delete by index ? "R.Wieser" <address@is.invalid> - 2025-11-06 10:03 +0100
    Re: VBScript dictionary delete by index ? "R.Wieser" <address@is.invalid> - 2025-11-06 15:03 +0100
      Re: VBScript dictionary delete by index ? "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2025-11-06 22:12 +0800
        Re: VBScript dictionary delete by index ? solved "R.Wieser" <address@is.invalid> - 2025-11-06 16:09 +0100
          Re: VBScript dictionary delete by index ? solved "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2025-11-08 11:52 +0800
            Re: VBScript dictionary delete by index ? solved "R.Wieser" <address@is.invalid> - 2025-11-08 09:07 +0100
              Re: VBScript dictionary delete by index ? solved "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2025-11-08 22:55 +0800
                Re: VBScript dictionary delete by index ? solved "R.Wieser" <address@is.invalid> - 2025-11-08 16:49 +0100
    Re: VBScript dictionary delete by index ? C-Sharp User <csharp@invalid.invalid> - 2025-11-06 22:40 +0000
      Re: VBScript dictionary delete by index ? "R.Wieser" <address@is.invalid> - 2025-11-07 09:15 +0100

#22 — VBScript dictionary delete by index ?

From"R.Wieser" <address@is.invalid>
Date2025-11-06 10:03 +0100
SubjectVBScript dictionary delete by index ?
Message-ID<10eho89$10spk$1@dont-email.me>
Hello all,

I've got a vbscript Dictionary object, and need to be able to delete a 
key-item pair by its index.  Can it be done and if so, how is it expressed ?

Remark: I can use

oDict.remove oDict.keys()(Index)

, but that takes two steps and is doing more work than is needed (which also 
excludes converting to a pair of arrays and converting it back afterwards by 
the way :-) )

Regards,
Rudy Wieser

[toc] | [next] | [standalone]


#23

From"R.Wieser" <address@is.invalid>
Date2025-11-06 15:03 +0100
Message-ID<10ei9rv$15pcl$1@dont-email.me>
In reply to#22
Just now I realized that I took it for granted that I can't "for each" a 
dictionary in a reverse order (the reason for the reverse order is because I 
want to delete items)

So, I'm asking now : is it possible to do a "for each key in oDict" from the 
last to first item ?

I just googeled for it and didn't find anything, but that doesn't say 
everything.

Regards,
Rudy Wieser

[toc] | [prev] | [next] | [standalone]


#24

From"Mr. Man-wai Chang" <toylet.toylet@gmail.com>
Date2025-11-06 22:12 +0800
Message-ID<10eiacg$15l4r$6@toylet.eternal-september.org>
In reply to#23
On 6/11/2025 10:03 pm, R.Wieser wrote:
 >
 > I've got a vbscript Dictionary object, and need to be able to delete a
 > key-item pair by its index.  Can it be done and if so, how is
 > it expressed ?
 >
 > Remark: I can use
 >
 > oDict.remove oDict.keys()(Index)
 >
> Just now I realized that I took it for granted that I can't "for each" a
> dictionary in a reverse order (the reason for the reverse order is because I
> want to delete items)
> 
> So, I'm asking now : is it possible to do a "for each key in oDict" from the
> last to first item ?
> 
> I just googeled for it and didn't find anything, but that doesn't say
> everything.
> 


If you can use array index to access oDict, you first count the number 
of items in oDict, then use a for loop from number of items back to 1.

-- 
   @~@   Simplicity is Beauty! Remain silent! Drink, Blink, Stretch!
  / v \  May the Force and farces be with you! Live long and prosper!!
/( _ )\ https://sites.google.com/site/changmw/
   ^ ^   https://github.com/changmw/changmw

[toc] | [prev] | [next] | [standalone]


#25 — Re: VBScript dictionary delete by index ? solved

From"R.Wieser" <address@is.invalid>
Date2025-11-06 16:09 +0100
SubjectRe: VBScript dictionary delete by index ? solved
Message-ID<10eidms$16s68$1@dont-email.me>
In reply to#24
Mr. Man-wai Chang,

> If you can use array index to access oDict, you first count the number of 
> items in oDict, then use a for loop from number of items back to 1.

That is what I'm curremtly doing :

for i=oDict.count-1 to 0 step -1

I still need to use

oDict.remove oDict.keys()(Index)

to remove the item, which does a double resolving (from index to key, and 
than use the key to delete the key-item pair

If I can use a "for each" in reverse than I already have the key (and don't 
need the index)



Arrrgggh...

I just realized I took it for granted that deleting a key-item pair while 
inside a standard "for each" loop would cause an error/crash (looping beyond 
the last item).

A quick test shows that that doesn't happen (and no entries are skipped). 
iow, I saw a problem where none is present.

I can't even really say that the problem is solved, as it didn't exist in 
the first place. :-( :-)

Regards,
Rudy Wieser

[toc] | [prev] | [next] | [standalone]


#28 — Re: VBScript dictionary delete by index ? solved

From"Mr. Man-wai Chang" <toylet.toylet@gmail.com>
Date2025-11-08 11:52 +0800
SubjectRe: VBScript dictionary delete by index ? solved
Message-ID<10emept$2av65$1@toylet.eternal-september.org>
In reply to#25
On 6/11/2025 11:09 pm, R.Wieser wrote:
 >
> If I can use a "for each" in reverse than I already have the key (and don't
> need the index)
> 

You can first sort oDict in reverse order, but that's quite silly. :)

 > I just realized I took it for granted that deleting a
 > key-item pair while inside a standard "for each" loop
 > would cause an error/crash (looping beyond the last item).


That surely could be a problem for linked list, not array index.

-- 
   @~@   Simplicity is Beauty! Remain silent! Drink, Blink, Stretch!
  / v \  May the Force and farces be with you! Live long and prosper!!
/( _ )\ https://sites.google.com/site/changmw/
   ^ ^   https://github.com/changmw/changmw

[toc] | [prev] | [next] | [standalone]


#29 — Re: VBScript dictionary delete by index ? solved

From"R.Wieser" <address@is.invalid>
Date2025-11-08 09:07 +0100
SubjectRe: VBScript dictionary delete by index ? solved
Message-ID<10emtp7$2eep5$1@dont-email.me>
In reply to#28
Mr. Man-wai Chang,

>> If I can use a "for each" in reverse than I already have the key (and 
>> don't
>> need the index)
>
> You can first sort oDict in reverse order, but that's quite silly. :)

Yes, and it would not solve anything. :-|   (the contents are not the 
problem)

> > I just realized I took it for granted that deleting a
> > key-item pair while inside a standard "for each" loop
> > would cause an error/crash (looping beyond the last item).
>
> That surely could be a problem for linked list, not array index.

I'm not at all sure about the first, as the "next" pointer (for the 
iteration) doesn't need to be replaced (assuming that its retrieved and 
stored somewhere as part of advancing to the current item).

The problem with a "for i=0 to dict.count-1" loop is that it both will skip 
the item after the one thats being removed, and it will not adjust the "upto 
here" part of the for loop to the new size of the array.

Looping backwards makes both of those problems disappear.

... and thats what my head was - unneeded - stuck on. :-\

Regards,
Rudy Wieser

[toc] | [prev] | [next] | [standalone]


#30 — Re: VBScript dictionary delete by index ? solved

From"Mr. Man-wai Chang" <toylet.toylet@gmail.com>
Date2025-11-08 22:55 +0800
SubjectRe: VBScript dictionary delete by index ? solved
Message-ID<10enlku$2kl2i$1@toylet.eternal-september.org>
In reply to#29
On 8/11/2025 4:07 pm, R.Wieser wrote:
> 
> I'm not at all sure about the first, as the "next" pointer (for the
> iteration) doesn't need to be replaced (assuming that its retrieved and
> stored somewhere as part of advancing to the current item).
> 
> The problem with a "for i=0 to dict.count-1" loop is that it both will skip
> the item after the one thats being removed, and it will not adjust the "upto
> here" part of the for loop to the new size of the array.

You can always create a small program with a simpler oDict to find out. 
Anyway, glad you solved the problem.

-- 
   @~@   Simplicity is Beauty! Remain silent! Drink, Blink, Stretch!
  / v \  May the Force and farces be with you! Live long and prosper!!
/( _ )\ https://sites.google.com/site/changmw/
   ^ ^   https://github.com/changmw/changmw

[toc] | [prev] | [next] | [standalone]


#31 — Re: VBScript dictionary delete by index ? solved

From"R.Wieser" <address@is.invalid>
Date2025-11-08 16:49 +0100
SubjectRe: VBScript dictionary delete by index ? solved
Message-ID<10enoqc$2lu4m$1@dont-email.me>
In reply to#30
Mr. Man-wai Chang,

> You can always create a small program with a simpler oDict to find out.

:-) Thats what I always do, and how I found that a "dict.remove" inside a 
"For each" loop actually works.

> Anyway, glad you solved the problem.

I already had a (clumsy) solution, but was looking for a simpler one.  And 
good that I did. :-)

Thanks.

Regards,
Rudy Wieser

[toc] | [prev] | [next] | [standalone]


#26

FromC-Sharp User <csharp@invalid.invalid>
Date2025-11-06 22:40 +0000
Message-ID<10ej8cc$273o7$1@paganini.bofh.team>
In reply to#22
On 06/11/2025 09:03, R.Wieser wrote:
> Hello all,
>
> I've got a vbscript Dictionary object, and need to be able to delete a
> key-item pair by its index.  Can it be done and if so, how is it expressed ?
>
> Remark: I can use
>
> oDict.remove oDict.keys()(Index)
>
> , but that takes two steps and is doing more work than is needed (which also
> excludes converting to a pair of arrays and converting it back afterwards by
> the way :-) )
>
> Regards,
> Rudy Wieser
>
>

How about CoPilot method:


Dim dict, key
Set dict = CreateObject("Scripting.Dictionary")

dict.Add "a", "apple"
dict.Add "b", "banana"
dict.Add "c", "cherry"

' Remove item by index (e.g., index 1)
key = dict.Keys()(1)
dict.Remove key

' Show remaining items
For Each key In dict.Keys
     WScript.Echo key & ": " & dict(key)
Next


> ' Create an array (range of values)
> Dim arr
> arr = Array(1, 2, 3, 4, 5)
>
> ' Define the start and end index for reversing
> Dim i
> Dim reversedArr
> ReDim reversedArr(UBound(arr)) ' Create a new array to store the 
> reversed values
>
> ' Reverse the array using a loop
> For i = 0 To UBound(arr)
>     reversedArr(i) = arr(UBound(arr) - i)
> Next
>
> ' Output the reversed array
> For i = 0 To UBound(reversedArr)
>     WScript.Echo reversedArr(i)
> Next

[toc] | [prev] | [next] | [standalone]


#27

From"R.Wieser" <address@is.invalid>
Date2025-11-07 09:15 +0100
Message-ID<10ek9sl$1nbjm$1@dont-email.me>
In reply to#26
C-Sharp User,

> How about CoPilot method:
...
> key = dict.Keys()(1)
> dict.Remove key

well, I mentioned that I'm already using it :

>> oDict.remove oDict.keys()(Index)

I would like to skip having to resolve from index to key first.

But it turns out that that "dict.Remove" can be used in a "for each key in 
dict" loop, without it skipping entries or trying to access the dict beyond 
its end.

Regards,
Rudy Wieser

[toc] | [prev] | [standalone]


Back to top | Article view | alt.comp.lang.vbscript


csiph-web