Page 1 of 1 1
Topic Options
#176359 - 2007-05-21 10:45 AM Adding items to an Array during a loop
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
I've been trying to add items to an array during a loop without success, in VB there is the Array.Add() but apprantly this doesn't work in KiX. Can anyone shed some light on this for me ?

To be more exact, I'm trying to do this without the Split() Join() functions. Here's my example test code
 Code:
Dim $fso, $txt
Dim $Array[5]
$fso = CreateObject("Scripting.FileSystemObject")
$txt = $fso.OpenTextFile("c:\boot.ini",Not 0)

While Not $txt.AtEndOfStream
  ;? $txt.ReadLine
  $Array.Add($txt.ReadLine)
Loop

$txt.Close
For Each $item in $Array
  ? "Line: " + $item
Next

Top
#176364 - 2007-05-21 01:20 PM Re: Adding items to an Array during a loop [Re: Arend_]
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
You could do this:

 Code:
Dim $fso, $txt
Dim $Array[5]
$fso = CreateObject("Scripting.FileSystemObject")
$txt = $fso.OpenTextFile("c:\boot.ini",Not 0)

While Not $txt.AtEndOfStream
  $high = UBound($array) + 1
  ReDim Preserve $Array[$high]
  $Array[$high] = $txt.ReadLine
Loop

$txt.Close
For Each $item in $Array
  ? "Line: " + $item
Next


It's the way it is usually done in vbscript, but I think I remember a thread stating that this trick is really slow in Kix.
_________________________
The Code is out there

Top
#176366 - 2007-05-21 01:39 PM Re: Adding items to an Array during a loop [Re: DrillSergeant]
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
Or you use the arraylist...
 Code:
Break On Cls

Dim $fso, $txt

$Array = CreateObject("System.Collections.ArrayList")

$fso = CreateObject("Scripting.FileSystemObject")
$txt = $fso.OpenTextFile("c:\boot.ini",Not 0)

While Not $txt.AtEndOfStream
  $Array.Add $txt.ReadLine + @CrLf
Loop

$txt.Close
For Each $item in $Array
  ? "Line: " + $item
Next
_________________________
The Code is out there

Top
#176367 - 2007-05-21 02:26 PM Re: Adding items to an Array during a loop [Re: DrillSergeant]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
For this particular application you could get away with ReadAll and a single split:

 Code:
Dim $fso, $txt
 
$fso = CreateObject("Scripting.FileSystemObject")
$txt = $fso.OpenTextFile("c:\boot.ini",Not 0)
 
$Array=Split($txt.ReadAll,@CRLF)
 
$txt.Close
 
For Each $item in $Array
  ? "Line: " + $item
Next

Top
#176372 - 2007-05-21 04:01 PM Re: Adding items to an Array during a loop [Re: Richard H.]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Richard, I wrote this particular code as per example to illustrate what I ment \:\) I know about the ReadAll, thanks though \:\)

Drill Sergeant: Your second suggestion works perfect \:\)
I've implented it like this:
 Code:
Dim $fso, $txt, $item, $Array
$fso = CreateObject("Scripting.FileSystemObject")
$Array = CreateObject("System.Collections.ArrayList")
$txt = $fso.OpenTextFile("c:\boot.ini",Not 0)

While Not $txt.AtEndOfStream
  $Array.Add($txt.ReadLine)
Loop

$txt.Close
For Each $item in $Array
  ? "Line: " + $item
Next


I still find it weird that there is no builtin kix way to Add stuff to an array. Besides, this way works fine however it does require the dotnet framework.

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.031 seconds in which 0.011 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org