Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare first 2 entries in Array

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
Hi,

A simple one I think. Is there a way I can echo the first 2 entries in an array:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\Scripts\results.txt", 1)

'check through text file and create 1d array
Do Until objFile1.AtEndOfStream
Redim Preserve arrFileOut(i)
arrFileOut(i) = objFile1.ReadLine
i = i + 1
Loop

'Read through array from last line to 1st line
For l = Ubound(arrFileOut) to LBound(arrFileOut) Step -1
errorline = arrFileOut(l)
wscript.echo errorline
next

This array echo from the bottom up.....

Thaks
 
You're wasting time and memory resizing your array. You can use a Dictionary instead...

Code:
Dim oFSO, oTSIn, oDict, iCtr, iKey

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTSIn = oFSO.OpenTextFile("datafile.txt",1)
Set oDict = CreateObject("Scripting.Dictionary")
iCtr = 0

Do
	oDict.Add iCtr, oTSIn.ReadLine
	iCtr = iCtr + 1
Loop Until oTSIn.AtEndOfStream

[green]' Echo header lines first [/green]
WScript.Echo oDict.Item(0)
WScript.Echo oDict.Item(1)

[green]' Start at n and go back to 2, echoing each item back[/green]
For iKey = iCtr To 2 Step -1
	WScript.Echo oDict.Item(iKey)
Next


PSC
[—] CCNP [•] CCSP [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
>Is there a way I can echo the first 2 entries in an array
[tt]
dim imax
if ubound(a)>1 then imax=1 else imax=ubound(a)
For l = 0 to imax
errorline = arrFileOut(l)
wscript.echo errorline
next
[/tt]
 
amendment
>[self]if ubound(a)>1 then imax=1 else imax=ubound(a)
should be read, sure, like this.
[tt]if ubound(arrFileOut)>1 then imax=1 else imax=ubound(arrFileOut)[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top