abdulrahmanok

want to make script smaller

17 posts in this topic

Hi  this is my  code :
 

Local $sFileRead = FileReadLine($hFileOpen, 1)
  Local $sFileRead2 = FileReadLine($hFileOpen, 2)
    Local $sFileRead3= FileReadLine($hFileOpen, 3)
      Local $sFileRead4 = FileReadLine($hFileOpen, 4)

and 25 variable like this
.............

I wrote it like this because I need each variable for specific event like this :

_GUICtrlListBox_AddString($idListBox, StringFormat("Houres For 1 : "))
        _GUICtrlListBox_AddString($idListBox, StringFormat($sFileRead))
_GUICtrlListBox_AddString($idListBox, StringFormat("Houres For 2 : "))
_GUICtrlListBox_AddString($idListBox, StringFormat($sFileRead2))

So i need to read each line from text file and put it in different variables
Sorry for my English I hope you understand what i mean :)

Share this post


Link to post
Share on other sites



FileReadToArray and then just use the array elements instead of individual variables. If you don't know how to use arrays then go here to learn about them.


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
17 minutes ago, BrewManNH said:

FileReadToArray and then just use the array elements instead of individual variables. If you don't know how to use arrays then go here to learn about them.


Thanks for your response  i actually  tried this before  but my try was very noob ...
 

$sFilePath = @ScriptDir & "/1.txt"
 ; Open the file for reading and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
Local $arr[3] =   [$sFileRead = FileReadLine($hFileOpen, 1), $sFileRead = FileReadLine($hFileOpen, 1), $sFileRead = FileReadLine($hFileOpen, 1)]

For $i = 0 to 3 - 1 ; We have an array with three elements but the last index is two.
_GUICtrlListBox_AddString($idListBox, StringFormat($sFileRead))
Next

Very difficult :(

Share this post


Link to post
Share on other sites
3 minutes ago, abdulrahmanok said:

Very difficult :(

Use FileReadToArray as @BrewManNH suggested.

Share this post


Link to post
Share on other sites

I said to use FileReadToArray. 


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
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <file.au3>
Example()

Func Example()
 Local $htry, $sFilePath = @ScriptDir & "/1.txt"
    Local $hFileOpen, $sFilePath = @ScriptDir & "/try.txt"

     Local $sFileRead = FileReadLine($hFileOpen, 1)
    Local $sFileRead2 = FileReadLine($hFileOpen, 2)
    Local $sFileRead3 = FileReadLine($hFileOpen, 3)
    Local $sFileRead4 = FileReadLine($hFileOpen, 4)
    Local $sFileRead5 = FileReadLine($hFileOpen, 5)
    Local $sFileRead6 = FileReadLine($hFileOpen, 6)
    Local $sFileRead7 = FileReadLine($hFileOpen, 7)
    Local $sFileRead8 = FileReadLine($hFileOpen, 8)
    Local $sFileRead9 = FileReadLine($hFileOpen, 9)
    Local $sFileRead10 = FileReadLine($hFileOpen, 10)
    Local $sFileRead11 = FileReadLine($hFileOpen, 11)
    Local $sFileRead12 = FileReadLine($hFileOpen, 12)
    Local $sFileRead13 = FileReadLine($hFileOpen, 13)
    Local $sFileRead14 = FileReadLine($hFileOpen, 14)
    Local $sFileRead15 = FileReadLine($hFileOpen, 15)
    ; Create 1D array
    Local $aArray[] = [$sFileRead ,$sFileRead2,$sFileRead3,$sFileRead4]
    ; Write it to file
    _FileWriteFromArray($sFilePath, $aArray, Default, Default, @CRLF)
    Sleep(1000)

    ; Re-read it - with count
    _FileReadToArray($sFilePath, $hFileOpen)
    _ArrayDisplay($hFileOpen, "1D array - count", Default, 8)

    ; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




EndFunc   ;==>Example

maybe almost ...............

Share this post


Link to post
Share on other sites

You seriously should just use FileReadToArray and dispense with all of the FileReadLine stuff, it is totally unnecessary.

I'm not sure how that is going over your head, but using FileReadLine over and over again to then write a file to be read by FRTA is bizarre to say the least. Also, you don't need _FileReadToArray, you should be using FileReadToArray


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
On 10/10/2016 at 5:28 PM, BrewManNH said:

You seriously should just use FileReadToArray and dispense with all of the FileReadLine stuff, it is totally unnecessary.

I'm not sure how that is going over your head, but using FileReadLine over and over again to then write a file to be read by FRTA is bizarre to say the least. Also, you don't need _FileReadToArray, you should be using FileReadToArray

After This Post :

https://www.autoitscript.com/forum/topic/185030-read-date-from-menu/#comment-1328997

I learned a little about Arrays And I Created This code :

Local $aArray = FileReadToArray(@ScriptDir&"\Calc.txt")
    If @error Then
    Else
        For $i = 0 To UBound($aArray) - 1 ; Loop through the array.
; Read File To Array is Done
        ExitLoop
        Next

 But My Problem Now is to Set This Array Values To ListBox :
 

_GUICtrlListBox_AddString($idListBox, StringFormat("Houres For 1 : "))
        _GUICtrlListBox_AddString($idListBox, StringFormat($sFileRead))
_GUICtrlListBox_AddString($idListBox, StringFormat("Houres For 2 : "))
_GUICtrlListBox_AddString($idListBox, StringFormat($aArray[0])) ; This is my Try 
_GUICtrlListBox_AddString($idListBox, StringFormat("Houres For 3 : "))
    _GUICtrlListBox_AddString($idListBox, StringFormat($aArray[1]))
_GUICtrlListBox_AddString($idListBox, StringFormat("Houres For 4 : "))
    _GUICtrlListBox_AddString($idListBox, StringFormat($sFileRead4))
_GUICtrlListBox_AddString($idListBox, StringFormat("Houres For 5 : "))
    _GUICtrlListBox_AddString($idListBox, StringFormat($sFileRead5))
_GUICtrlListBox_AddString($idListBox, StringFormat("Houres For 6 : "))
    _GUICtrlListBox_AddString($idListBox, StringFormat($sFileRead6))

 

Share this post


Link to post
Share on other sites

Should look something like this (untested)

Local $aArray = FileReadToArray(@ScriptDir&"\Calc.txt")
    If @error Then
    Else
        For $i = 0 To UBound($aArray) - 1 ; Loop through the array.

            _GUICtrlListBox_AddString($idListBox, StringFormat($aArray[$i])) ; should look more like this

; Read File To Array is Done
;~         ExitLoop  Dont exit loop unless there is an error handler
        Next

 

Share this post


Link to post
Share on other sites

#10 ·  Posted

11 minutes ago, l3ill said:

Should look something like this (untested)

Local $aArray = FileReadToArray(@ScriptDir&"\Calc.txt")
    If @error Then
    Else
        For $i = 0 To UBound($aArray) - 1 ; Loop through the array.

            _GUICtrlListBox_AddString($idListBox, StringFormat($aArray[$i])) ; should look more like this

; Read File To Array is Done
;~         ExitLoop  Dont exit loop unless there is an error handler
        Next

 

Niiice It's worked ! 
But It extract Array Like This :
 

1

2

3

4

and i want it like This :
 

first day

1
second day

2
third day

3
fourth day

4

 

Share this post


Link to post
Share on other sites

#12 ·  Posted

Just now, l3ill said:

I need an example of you calc.txt

that it :

1

2

3

4

5

but i want add line Before "1" Called Day

And Before "2" Called Day

....

Share this post


Link to post
Share on other sites

#13 ·  Posted

Try something like this

_GUICtrlListBox_AddString($idListBox, "Hours for: " & ($aArray[$i]))

            _GUICtrlListBox_AddString($idListBox, StringFormat($aArray[$i])) ; should look more like this

Change "Hours For:" to anything you want.

First, Second etc will be more difficult to program, although there is probably a udf for it somewhere...

Share this post


Link to post
Share on other sites

#14 ·  Posted

6 minutes ago, l3ill said:

I need an example of your calc.txt

and post your entire updated code please...

Finally Did it by Add another Array :

GUICreate("List Box Add String", 400, 296)
    $idListBox = GUICtrlCreateList("", 2, 2, 396, 296, BitOR($WS_BORDER, $WS_VSCROLL, $LBS_NOTIFY, $LBS_DISABLENOSCROLL, $WS_HSCROLL))
        GUISetState(@SW_SHOW)
    Local $aArray2 = FileReadToArray(@ScriptDir&"\try.txt")

 
        For $i = 0 To UBound($aArray2) - 1 ; Loop through the array.

        Next

    Local $aArray = FileReadToArray(@ScriptDir&"\Calc.txt")
    If @error Then
    Else
        For $i = 0 To UBound($aArray) - 1 ; Loop through the array.

            _GUICtrlListBox_AddString($idListBox, StringFormat($aArray[$i])) ; should look more like this
 _GUICtrlListBox_AddString($idListBox, StringFormat($aArray2[$i])) ; should look more like this

Ty Very Much For Help Finally my Script is clean now :)

Share this post


Link to post
Share on other sites

#16 ·  Posted

2 minutes ago, l3ill said:

o:) Now run Tidy and see what really clean looks like :lmao:

:drool::drool::drool:

Ty Very Much !

Share this post


Link to post
Share on other sites

#17 ·  Posted

5 minutes ago, l3ill said:

Try something like this

_GUICtrlListBox_AddString($idListBox, "Hours for: " & ($aArray[$i]))

            _GUICtrlListBox_AddString($idListBox, StringFormat($aArray[$i])) ; should look more like this

Change "Hours For:" to anything you want.

First, Second etc will be more difficult to program, although there is probably a udf for it somewhere...

Nice Idea :)

 

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