• VBScript dictionary delete by index ?

    From R.Wieser@address@is.invalid to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Thu Nov 6 10:03:00 2025
    From Newsgroup: alt.windows7.general

    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


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From R.Wieser@address@is.invalid to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Thu Nov 6 15:03:39 2025
    From Newsgroup: alt.windows7.general

    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


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mr. Man-wai Chang@toylet.toylet@gmail.com to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Thu Nov 6 22:12:31 2025
    From Newsgroup: alt.windows7.general

    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
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From R.Wieser@address@is.invalid to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Thu Nov 6 16:09:12 2025
    From Newsgroup: alt.windows7.general

    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


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From C-Sharp User@csharp@invalid.invalid to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Thu Nov 6 22:40:40 2025
    From Newsgroup: alt.windows7.general

    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
    -a-a-a 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)
    -a-a-a reversedArr(i) = arr(UBound(arr) - i)
    Next

    ' Output the reversed array
    For i = 0 To UBound(reversedArr)
    -a-a-a WScript.Echo reversedArr(i)
    Next


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From R.Wieser@address@is.invalid to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Fri Nov 7 09:15:50 2025
    From Newsgroup: alt.windows7.general

    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


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mr. Man-wai Chang@toylet.toylet@gmail.com to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Sat Nov 8 11:52:26 2025
    From Newsgroup: alt.windows7.general

    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
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From R.Wieser@address@is.invalid to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Sat Nov 8 09:07:57 2025
    From Newsgroup: alt.windows7.general

    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


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mr. Man-wai Chang@toylet.toylet@gmail.com to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Sat Nov 8 22:55:23 2025
    From Newsgroup: alt.windows7.general

    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
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From R.Wieser@address@is.invalid to alt.comp.lang.vbscript,alt.comp.os.windows-xp,alt.windows7.general on Sat Nov 8 16:49:27 2025
    From Newsgroup: alt.windows7.general

    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


    --- Synchronet 3.21a-Linux NewsLink 1.2