×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Delete records from VBS array

Delete records from VBS array

Delete records from VBS array

(OP)
Hi community!

Trying to figure out with the following task:

I have source 2-dimensional array in VBS script, which i need to read in the loop and save only those records, which have the max TimeStamp per each Name:
Source:



Name Type TimeStamp
Andrew Type1 45678
Andrew Type2 45679
Andrew Type1 46678
Andrew Type2 45660
Mike Type3 6543
Mike Type4 653
Tom Type1 643


Expected array:


Name Type TimeStamp
Andrew Type1 46678
Mike Type3 6543
Tom Type1 643


I tried to use a classic way of fetching the max element using following code, but for not a bit confused how to apply it for 2-dim array:

Dim arr,max

arr=array(6,5,8,2,0,9)

max=arr(0)

for i=0 to ubound(arr)

if max< arr(i) then
max=arr(i)

end if

next


Thanks for your help!

RE: Delete records from VBS array

Hi Andrew91,

Here is an example which reads your input array, and creates an output array which only contains for every name one record with maximum timestamp:

CODE

' input array
my_inp_array = Array(Array("Andrew", "Type1", 45678),_
                     Array("Andrew", "Type2", 45679),_
                     Array("Andrew", "Type1", 46678),_
                     Array("Andrew", "Type2", 45660),_
                     Array("Mike", "Type3", 6543),_
                     Array("Mike", "Type4", 653),_
                     Array("Tom", "Type1", 643))

wscript.echo("Input Array:")
call show_array(my_inp_array)
                 
wscript.echo("Processing ...")

' create Dictionary object
set my_hash = CreateObject("Scripting.Dictionary")

for j=0 to Ubound(my_inp_array)
  'wscript.echo((j+1) & ": " & my_inp_array(j)(0) & "- " & my_inp_array(j)(2))
  ' get key and timestamp value
  key = my_inp_array(j)(0)
  timestamp = my_inp_array(j)(2)

  ' add records to hash (i.e. dictionary)
  if my_hash.exists(key) then
    if timestamp > my_hash.item(key)(2) then
      ' replace existing record with record which has greater timestamp
      my_hash.item(key) = my_inp_array(j) 
    end if
  else
    my_hash.item(key) = my_inp_array(j)
  end if
next

' create output array dynamically  
dim my_out_array()
' set the dimension to number of keys in the hash
n = Ubound(my_hash.keys)
redim my_out_array(n)

' convert hash records into array records 
j = 0
for each key in my_hash
  ' wscript.echo(key & " ==> " & my_hash.item(key)(2))
  my_out_array(j) = my_hash.item(key)
  j = j + 1
next

wscript.echo("... Done.")

' show results
wscript.echo("Output Array:")
call show_array(my_out_array)

' -----------------------------------------------------------
sub show_array(a)
  for k=0 to Ubound(a)
    wscript.echo("Name: " & a(k)(0) & vbTab & _
                 "Type: " & a(k)(1) & vbTab & _
                 "TimeStamp: " & a(k)(2))
  next
end sub 

The output is:

CODE

u:\Work\VBscript>cscript /NoLogo andrew91.vbs
Input Array:
Name: Andrew    Type: Type1     TimeStamp: 45678
Name: Andrew    Type: Type2     TimeStamp: 45679
Name: Andrew    Type: Type1     TimeStamp: 46678
Name: Andrew    Type: Type2     TimeStamp: 45660
Name: Mike      Type: Type3     TimeStamp: 6543
Name: Mike      Type: Type4     TimeStamp: 653
Name: Tom       Type: Type1     TimeStamp: 643
Processing ...
... Done.
Output Array:
Name: Andrew    Type: Type1     TimeStamp: 46678
Name: Mike      Type: Type3     TimeStamp: 6543
Name: Tom       Type: Type1     TimeStamp: 643 

RE: Delete records from VBS array

You can replace all of

CODE

' set the dimension to number of keys in the hash
n = UBound(my_hash.Keys)
ReDim my_out_array(n)

' convert hash records into array records
j = 0
For Each Key In my_hash
  wscript.echo(key & " ==> " & my_hash.item(key)(2))
  my_out_array(j) = my_hash.Item(Key)
  j = j + 1
Next 

with

CODE

my_out_array = my_hash.Items 

RE: Delete records from VBS array

Thanks strongm

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close