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 - How to insert an entry at first position of a Scripting.Dictionary ?

Reply
 
Old 03-16-2009   #1 (permalink)
Thomas Lebrecht


 
 

How to insert an entry at first position of a Scripting.Dictionary ?

When I create a Scripting.Dictionary with a command like

Set objLines = CreateObject("Scripting.Dictionary")

I can later add entries at the end with a command

objLines.Add strLine, True

But what if I want to insert them at the first position (and shift the existing lines
one position to the end).

A similar question applies to ArrayLists:

Set arrList = CreateObject("System.Collections.ArrayList")

How can I achieve an insert at the first position for this Object?

Thomas


My System SpecsSystem Spec
Old 03-16-2009   #2 (permalink)
T Lavedas


 
 

Re: How to insert an entry at first position of aScripting.Dictionary ?

On Mar 16, 3:06*am, t.lebre...@xxxxxx (Thomas Lebrecht) wrote:
Quote:

> When I create a Scripting.Dictionary with a command like
>
> Set objLines = CreateObject("Scripting.Dictionary")
>
> I can later add entries at the end with a command
>
> objLines.Add strLine, True
>
> But what if I want to insert them at the first position (and shift the existing lines
> one position to the end).
>
> A similar question applies to ArrayLists:
>
> Set arrList = CreateObject("System.Collections.ArrayList")
>
> How can I achieve an insert at the first position for this Object?
>
> Thomas
Can you explain why you want/need to do this, since it is my belief
that a fundamental purpose of a dictionary is to make such an action
unnecessary. Rather, the Key is used to address items in the
dictionary, irrespective of their relative position in it.

Without a clearer understanding of the exact objective, the best I can
say is that the dictionary will need to be rebuilt. That is,
temporary arrays of keys and items would need to be made from the
current state of the dictionary, the dictionary emptied, the new first
items added and then the arrays of keys and items added back in. This
might be fine for simple lists, but would get slower and slower as the
dictionary grew.

Sorting an ArrayList might provide what you need, if a suitable index
arrangement can be constructed, but more information about the details
of your problem and objective are needed, at least as far as I am
concerned.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
My System SpecsSystem Spec
Old 03-16-2009   #3 (permalink)
Hz


 
 

Re: How to insert an entry at first position of a Scripting.Dictionary?

Thomas Lebrecht wrote:
Quote:

> When I create a Scripting.Dictionary with a command like
>
> Set objLines = CreateObject("Scripting.Dictionary")
>
> I can later add entries at the end with a command
>
> objLines.Add strLine, True
>
> But what if I want to insert them at the first position (and shift the existing lines
> one position to the end).
>
> A similar question applies to ArrayLists:
>
> Set arrList = CreateObject("System.Collections.ArrayList")
>
> How can I achieve an insert at the first position for this Object?
>
> Thomas
>
You can use a disconnected recordset. Something like:

Const adUseClient = 3
Const adOpenStatic = 3
Const adLockBatchOptimistic = 4
Const adInteger = 3
Const adVarChar = 200
Const adDate = 7
Const adTypeBinary = 1

Set oRs = Server.CreateObject("ADODB.Recordset")
oRs.ActiveConnection = Nothing
oRs.CursorLocation = adUseClient
oRs.CursorType = adOpenStatic

oRs.Fields.Append "Name", adVarChar, 255
oRs.Fields.Append "Size", adInteger
oRs.Fields.Append "Description", adVarChar, 255

oRs.Open

oRs.AddNew
oRs(0) = "Anything.txt"
oRs(1) = 100
oRs(2) = "Anything really"

oRs.Sort = "Name" ' "Name DESC" for descending sort

--
..::[ Hz ]::.
My System SpecsSystem Spec
Old 03-16-2009   #4 (permalink)
Richard Mueller [MVP]


 
 

Re: How to insert an entry at first position of a Scripting.Dictionary ?


"T Lavedas" <tglbatch@xxxxxx> wrote in message
news:c871ea76-ce32-4779-93bc-5f33d0b27928@xxxxxx
On Mar 16, 3:06 am, t.lebre...@xxxxxx (Thomas Lebrecht) wrote:
Quote:

> When I create a Scripting.Dictionary with a command like
>
> Set objLines = CreateObject("Scripting.Dictionary")
>
> I can later add entries at the end with a command
>
> objLines.Add strLine, True
>
> But what if I want to insert them at the first position (and shift the
> existing lines
> one position to the end).
>
> A similar question applies to ArrayLists:
>
> Set arrList = CreateObject("System.Collections.ArrayList")
>
> How can I achieve an insert at the first position for this Object?
>
> Thomas
Can you explain why you want/need to do this, since it is my belief
that a fundamental purpose of a dictionary is to make such an action
unnecessary. Rather, the Key is used to address items in the
dictionary, irrespective of their relative position in it.

Without a clearer understanding of the exact objective, the best I can
say is that the dictionary will need to be rebuilt. That is,
temporary arrays of keys and items would need to be made from the
current state of the dictionary, the dictionary emptied, the new first
items added and then the arrays of keys and items added back in. This
might be fine for simple lists, but would get slower and slower as the
dictionary grew.

Sorting an ArrayList might provide what you need, if a suitable index
arrangement can be constructed, but more information about the details
of your problem and objective are needed, at least as far as I am
concerned.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/

-------------------

Entries in a dictionary object have no order. Likewise, items in a recordset
have no order, unless the recordset is sorted.

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


My System SpecsSystem Spec
Old 03-17-2009   #5 (permalink)
Cor Ligthert


 
 

RE: How to insert an entry at first position of a Scripting.Dictionary

Thomas,

I think that everybody thinks: Why is Thomas using a scripting directory and
codes like it is VB6 or VBA?"

Can you describe your problem so that you can get an answer in a truly
managed code way?

Cor


"Thomas Lebrecht" wrote:
Quote:

> When I create a Scripting.Dictionary with a command like
>
> Set objLines = CreateObject("Scripting.Dictionary")
>
> I can later add entries at the end with a command
>
> objLines.Add strLine, True
>
> But what if I want to insert them at the first position (and shift the existing lines
> one position to the end).
>
> A similar question applies to ArrayLists:
>
> Set arrList = CreateObject("System.Collections.ArrayList")
>
> How can I achieve an insert at the first position for this Object?
>
> Thomas
>
>
My System SpecsSystem Spec
Old 03-17-2009   #6 (permalink)
T Lavedas


 
 

Re: How to insert an entry at first position of aScripting.Dictionary ?

On Mar 16, 8:25*pm, "Richard Mueller [MVP]" <rlmueller-
nos...@xxxxxx> wrote:
Quote:

> "T Lavedas" <tglba...@xxxxxx> wrote in message
>
> news:c871ea76-ce32-4779-93bc-5f33d0b27928@xxxxxx
> On Mar 16, 3:06 am, t.lebre...@xxxxxx (Thomas Lebrecht) wrote:
>
>
>
Quote:

> > When I create a Scripting.Dictionary with a command like
>
Quote:

> > Set objLines = CreateObject("Scripting.Dictionary")
>
Quote:

> > I can later add entries at the end with a command
>
Quote:

> > objLines.Add strLine, True
>
Quote:

> > But what if I want to insert them at the first position (and shift the
> > existing lines
> > one position to the end).
>
Quote:

> > A similar question applies to ArrayLists:
>
Quote:

> > Set arrList = CreateObject("System.Collections.ArrayList")
>
Quote:

> > How can I achieve an insert at the first position for this Object?
>
Quote:

> > Thomas
>
> Can you explain why you want/need to do this, since it is my belief
> that a fundamental purpose of a dictionary is to make such an action
> unnecessary. *Rather, the Key is used to address items in the
> dictionary, irrespective of their relative position in it.
>
> Without a clearer understanding of the exact objective, the best I can
> say is that the dictionary will need to be rebuilt. *That is,
> temporary arrays of keys and items would need to be made from the
> current state of the dictionary, the dictionary emptied, the new first
> items added and then the arrays of keys and items added back in. *This
> might be fine for simple lists, but would get slower and slower as the
> dictionary grew.
>
> Sorting an ArrayList might provide what you need, if a suitable index
> arrangement can be constructed, but more information about the details
> of your problem and objective are needed, at least as far as I am
> concerned.
>
> Tom Lavedas
> ***********http://there.is.no.more/tglbatch/
>
> -------------------
>
> Entries in a dictionary object have no order. Likewise, items in a recordset
> have no order, unless the recordset is sorted.
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab -http://www.rlmueller.net
> --
I'm not certain what your point is. Conceptually, what you say is
true. That is what my first statement was trying to convey. However,
in a practical sense they have the order in which the data is 'added'
to the set. Witness the fact that the conversion of items and keys to
arrays results in the elements being in that order in each of the
arrays. For example, ...

Set objList = CreateObject("Scripting.Dictionary")

objList.Add "One", 1
objList.Add "Three", 3
objList.Add "Two", 2

arrKeys = objList.Keys
arrItems = objList.Items

wsh.echo Join(arrKeys, vbTab), vbNewline & Join(arrItems, vbTab)

If your point was that the OP was confused about the purpose and use
of a dictionary object, I would agree. Again, that is exactly what I
was trying to get across. If that's not your point, you'll need to
explain, because I'm too dense to understand it.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
My System SpecsSystem Spec
Old 03-18-2009   #7 (permalink)
Hz


 
 

Re: How to insert an entry at first position of a Scripting.Dictionary?

Oh, I think what you mean now. The text on a page in a book needs to be
in a certain order. Sorting it would make it unreadable.

I think this is what you want:

'###############################################################################
'# Run it using CScript
'#
'# Code by Hz - 2009
'###############################################################################

Dim oAl
Set oAl = CreateObject("System.Collections.ArrayList")
oAl.Add "First line"
oAl.Add "Second line"
oAl.Add "Third line"
oAl.Add "Fifth line"
Show oAl

'# Insert at the specified index (zero based)
oAl.Insert 3, "Fourth line"
Show oAl

Set oAl = Nothing

Sub Show(ByVal inArr)
Dim OutText
For Each Item In inArr
OutText = OutText & " " & Item
Next
Trim(OutText)
WScript.Echo OutText
End Sub

--
..::[ Hz ]::.
My System SpecsSystem Spec
Old 03-18-2009   #8 (permalink)
Hz


 
 

Re: How to insert an entry at first position of a Scripting.Dictionary?

I was too quick to post my code. Here is a better version

---------------------------

'###############################################################################
'# Run it using CScript
'#
'# Code by Hz - 2009
'###############################################################################

Option Explicit

Dim oAl
Set oAl = CreateObject("System.Collections.ArrayList")
oAl.Add "First line"
oAl.Add "Second line"
oAl.Add "Third line"
oAl.Add "Fifth line"
Show oAl

oAl.Insert 3, "Fourth line"
Show oAl

Set oAl = Nothing

Sub Show(ByVal inArr)
Dim OutText, Item
For Each Item In inArr
WScript.Echo Item
Next
WScript.Echo
End Sub

---------------------------

Output:
Quote:

>cscript.exe //nologo "C:\Users\Hz\Documents\Programming\ArrayList 2.vbs"
First line
Second line
Third line
Fifth line

First line
Second line
Third line
Fourth line
Fifth line
Quote:

>Exit code: 0
--
..::[ Hz ]::.
My System SpecsSystem Spec
Old 03-18-2009   #9 (permalink)
Hz


 
 

Re: How to insert an entry at first position of a Scripting.Dictionary?

Hz wrote:
Quote:

> Oh, I think what you mean now. The text on a page in a book needs to be
> in a certain order. Sorting it would make it unreadable.
Clarification:

The textLINES on a page in a book needs to be in a certain order.
Sorting them would make it unreadable.

--
..::[ Hz ]::.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Windows Mail - How Do I Edit or Delete Entry in User Dictionary Vista mail
Dictionary Vista mail
Dictionary<TKey,TValue> as a Dictionary key in C# 2.0 .NET General
UK Dictionary Live Mail
CD Insert/Jewel Case Insert Maker ignored? Vista music pictures video


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