Computer Hope

Software => Computer programming => Topic started by: scomp on June 17, 2008, 11:51:06 AM

Title: how to save a text file into an array
Post by: scomp on June 17, 2008, 11:51:06 AM
hi everyone,

                 I want to know how to save content of a file into an array.


               I want to save a full line into a particular position or address in an array.

e.g.

if a is an array

a(0) = save first line

a(1) = save second line

......

and it should be continue until all lines are saved.

Can anyone help me?

thank in advance
Title: Re: how to save a text file into an array
Post by: Sidewinder on June 17, 2008, 12:16:19 PM
Code: [Select]
@echo off
set idx=0
setlocal enabledelayedexpansion

for /f "tokens=* delims=" %%x in (test.txt) do (
call set /a idx=%%idx%%+1
call set array.%%idx%%=%%x
)

for /l %%x in (1, 1, %idx%) do (
echo !array.%%x!
)

Batch code does not support true arrays although you can mimic one. The first for shows how to load the array from a file. The second for shows the notation needed to access the array elements.

Happy Coding 8)
Title: Re: how to save a text file into an array
Post by: scomp on June 18, 2008, 02:00:03 AM
thanks sidewinder
Title: Re: how to save a text file into an array
Post by: Spasm on June 18, 2008, 04:23:54 AM
Yeah, what language are you programming in?