Sign in to follow this  
Followers 0
Reister

Basic Help reading files into arrays

11 posts in this topic

Hi,

I need to read a file and convert it into an array.

It is done using: _FileReadToArray("config.cfg", $configCfgAr)

Then I process it using:

For $element In $configCfgAr
if $i > 1 Then
 
  $Variable = StringSplit($element,":",1)
  $Datos[$i][1]=$Variable[1]
  $Datos[$i][2]=$Variable[2]
  $Parametros[$i][1] = GUICtrlCreateLabel($Variable[1], 40, $y, 250, 20)
  $Parametros[$i][2] = GUICtrlCreateInput($Variable[2], 252, $y, 500, 24)
 
EndIf
$i=$i+1
$y = $y + 20
Next

The issue is when there are empty lines in the config.cfg that I'm reading, the line $Datos[$i][2]=$Variable[2] gives Array Error.

How could I control that? How coulf I make sure that $Variable[2] exists or not?

THANKS!

Share this post


Link to post
Share on other sites



Take a look at IsArray.


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

If you encounter an empty line in config.cfg then StringSplit will return an error because there is not ":" in the variable.

So I would test for the error:

For $element In $configCfgAr
if $i > 1 Then 
  $Variable = StringSplit($element,":",1)
  If @error = 0 Then
    $Datos[$i][1]=$Variable[1]
    $Datos[$i][2]=$Variable[2]
    $Parametros[$i][1] = GUICtrlCreateLabel($Variable[1], 40, $y, 250, 20)
    $Parametros[$i][2] = GUICtrlCreateInput($Variable[2], 252, $y, 500, 24)
    $i=$i+1
  EndIf
EndIf
$y = $y + 20
Next

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

If you encounter an empty line in config.cfg then StringSplit will return an error because there is not ":" in the variable.

So I would test for the error:

For $element In $configCfgAr
if $i > 1 Then
  $Variable = StringSplit($element,":",1)
  If @error = 0 Then
    $Datos[$i][1]=$Variable[1]
    $Datos[$i][2]=$Variable[2]
    $Parametros[$i][1] = GUICtrlCreateLabel($Variable[1], 40, $y, 250, 20)
    $Parametros[$i][2] = GUICtrlCreateInput($Variable[2], 252, $y, 500, 24)
    $i=$i+1
  EndIf
EndIf
$y = $y + 20
Next

It didn't help, since it seems that with empty lines it doesn't return 0

And also, another question, how could I delete the@CRLF of the last line?

Edited by Reister

Share this post


Link to post
Share on other sites

Take a look at IsArray.

it didin't help also, because the variable is an array, but with empty parameters

Share this post


Link to post
Share on other sites

Then simply check for nothing?

$var1 = "test"
$var2 = ""

MsgBox(0, "", ($var1 == ""))
MsgBox(0, "", ($var2 == ""))

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

It didn't help, since it seems that with empty lines it doesn't return 0

That's right. For empty lines @error is set to 1. My code only processes the data if the springsplit was successful - which is not true for empty lines.

Don't understand why it shouldn't help.

Do you still get the array error on line "$Datos[$i][2]=$Variable[2]"?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Then simply check for nothing?

$var1 = "test"
$var2 = ""

MsgBox(0, "", ($var1 == ""))
MsgBox(0, "", ($var2 == ""))

It seems not working ;)

With arrays if I do this check:

if ($Variable[2] == "") Then $Datos[$i][2]=$Variable[2]

And $Variable[2] doesn't exist, then it crashes

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

That's right. For empty lines @error is set to 1. My code only processes the data if the springsplit was successful - which is not true for empty lines.

Don't understand why it shouldn't help.

Do you still get the array error on line "$Datos[$i][2]=$Variable[2]"?

Yes I have the error even using this.

Also, it is not valid for me, since the config.cfg could contain lines like:

PARAM1:VALUE

PARAM2:

PARAM3:VALUE

So, PARAM2, will be skipped, and I don't want it

Edited by Reister

Share this post


Link to post
Share on other sites

Try this:

For $element In $configCfgAr
    If $i > 1 Then
        $Variable = StringSplit($element, ":", 1)
        If IsArray($Variable) Then
;~   If @error = 0 Then
            $Datos[$i][1] = $Variable[1]
            $Parametros[$i][1] = GUICtrlCreateLabel($Variable[1], 40, $y, 250, 20)
            If $Variable[0] > 1 Then
                $Datos[$i][2] = $Variable[2]
                $Parametros[$i][2] = GUICtrlCreateInput($Variable[2], 252, $y, 500, 24)
            EndIf
            $i = $i + 1
        EndIf
    EndIf
    $y = $y + 20
Next

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

will the parameters/values always be separated by a colon?


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0