====== Collection Objects ======
===== Description =====
A collection is used to conceptually store a group of objects of the same type. You can use standard scripting methods to access the contents of a collection.
===== Properties =====
^Name^Type^Access^Description^
|Count|Long|Read|The number of objects stored in this.|
===== Methods =====
^Name ^Parameters^Description ^
|Item |Long|Returns the Item at this index. The items are zero-indexed. This method is implicitly called if you use the array operators in most scripting languages. For example: PacketStore(10) is internally translated to PacketStore.Item(10). |
Usage Notes
You can use the For..Next or the For Each method to iterate through a collection. Consult your scripting language for the corresponding methods. BScript and Ruby Examples are shown below.
===== Enumerating collection objects =====
==== VBScript ====
‘ Use the For Each statement
Set PacketStore = UnsniffDB.PacketIndex
For Each Packet In PacketStore
WScript.Echo
Packet.Description
Next
‘ Use the For statement
Set PacketStore = UnsniffDB.PacketIndex
NumPackets = PacketStore.Count
For I = 0 To NumPackets-1
Set Packet = PacketStore(I)
WScript.Echo Packet.Description
Next
==== Ruby ====
# Use the Count to loop
PacketStore = UnsniffDB.PacketIndex
Count = PacketStore.Count
(0..Count-1).each do |idx|
print PacketStore.Item(idx).Description
end
# Use the each block
Set PacketStore = UnsniffDB.PacketIndex
PacketStore.each { |packet| print packet.Description }
=== Using the Enumerable methods ===
Ruby has a very nifty way of working with collections called // Enumerable //. The objects exposed by Unsniff such as Packets, PDUs, Streams are actually C++ objects, so even though they have a method called @each@ they cannot be directly mixed in with // Enumerable //
You need to write a tiny helper class that delegates to the backend object. See below for example
# Tiny helper class adds Enumerable methods
class UWrap
include Enumerable
def initialize(w)
@wrapped=w
end
def each(&block)
@wrapped.each { |m| block.call(m) }
end
end
to use the object simply wrap your object with ''UWrap.new( // my object // )''
# returns array of PDU using the collect Enumerable method
UWrap.new(UnsniffDB.PacketIndex).collect do |p|
print p.Description
end