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

NameTypeAccessDescription
CountLongReadThe number of objects stored in this.

Methods

Name ParametersDescription
Item LongReturns 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).

<note> 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. </note>

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
unsniff/objectref/collectionobj.txt · Last modified: 2014/09/11 23:23 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki