Jump to content

Extract a piece of string from a file into a array


Recommended Posts

Guest Ancient Orange
Posted

Hi there!

I was wondering if anyone could help me with my little Problem here.

i have got a text file which contains a string seperated by a : followed by a number.

here is a small extraction

...

a:10

b:31

c:23

sfadsdf:12

...

is there a way to extract the number behind the : and alter its value?

the purpose is to count down the numbers.

The script is supposed to run once a day and reduce all numbers by one i started working on it this is how far i got:

Func Start()

$monitorf = FileOpen("C:\ca40\monitoring.txt", 0)

Global $HM

Global $aArray

Global $sFilePath

Global $sFilePath2

$sFilePath = "C:\ca40\monitoring.txt"

$sFilePath2 = "C:\ca40\monitoring.txt"

$HM = _FileCountLines( $sFilePath )

Msgbox (0, "",$HM)

Read()

EndFunc

Func Read()

$i = 0

While $i < $HM

$line = FileReadLine($sFilePath, 0)

$array = StringSplit( FileReadLine( $line ), ":" )

msgbox(0, "line",$line)

msgbox(0, "",($array[2]))

Wend

EndFunc

when i run the script i get the error that the array exceeds it's limits or something like that.
Posted

$line = FileReadLine($sFilePath, 0)
$array = StringSplit( FileReadLine( $line ), ":" )

FileReadLine requires a filename and optionally the line number. You're giving it a string containing the first line.

Try this (untested):

$linenumber = 1
$line = FileReadLine( $sFilePath, $linenumber )
if @error Then Exit; we've reached the end of the file or the file could not be opened
$array = StringSplit( $line , ":" )

This should give you an array:

$array[1] = 'a'

$array[2] = 10

Just increase $linenumber to get the next lines.

See the help for FileReadLine for further details.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...