Jump to content

Reading Text file and write into array


 Share

Recommended Posts

Hey all,

#include <File.au3>
#include <Array.au3>

$file = "D:\Dokumente\Tracklist.txt"
FileOpen($file, 0)
Global $arr
For $i = 1 to _FileCountLines($file)
    $line = FileReadLine($file, $i)
    $arr[_FileCountLines($file)]
    $arr[$i] = $line
Next
_ArrayDisplay($line)

#cs
_ArraySort($arr,
#ce

why doesn't it work? I wanted to use _ArraySort and let it be sorted alphabetically but I didn't even came that far.. 

I wanted to read each line then write it into an array with [linenumber] - line

Edited by Siryx
Link to comment
Share on other sites

 

#include <File.au3>
#include <Array.au3>

$file = "D:\Dokumente\Tracklist.txt"
FileOpen($file, 0)
Global $arr[1000]
For $i = 1 to _FileCountLines($file)
    $line = FileReadLine($file, $i)
    ReDim $arr[_FileCountLines($file)+1]
    $arr[$i] = $line
Next
_ArrayDisplay($arr)

#cs
_ArraySort($arr,
#ce

 

my bad.. but how to sort alphabetically? _arraysort helpfile doesn't help me and google doesn't help me either :/

do I have to turn the first character into ascii or sth and then sort then make it backwards? :x

nvm it does it automatically.. lol

Edited by Siryx
Link to comment
Share on other sites

Maybe FileListToArray FileReadToArray is what you're looking for?

Edited by BrewManNH
Wrong function posted

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

Link to comment
Share on other sites

nah its all good, I have a txt and I wanted to read in the lines into an array, which worked, now I used arraysort to make it sort alphabetically :) I found out by trial and error :P

#include <File.au3>
#include <Array.au3>

$file = "D:\Dokumente\Tracklist.txt"
FileOpen($file, 0)
Global $arr[1000]
ReDim $arr[_FileCountLines($file)+1]

For $i = 1 to _FileCountLines($file)
    $line = FileReadLine($file, $i)
    $arr[$i] = $line
Next
_ArraySort($arr)
_ArrayDisplay($arr)

another question, if I want to send $arr[n] to browser, which command should I use? Send ? 

Edited by Siryx
Link to comment
Share on other sites

I used the wrong function name in my previous post, I meant to say use FileRreadToArray instead.

 

another question, if I want to send $arr[n] to browser, which command should I use? Send ? 

​It depends on which browser you're using, AutoIt comes with the Internet Explorer UDFs that allow you to use it with IE, there's a FF UDF in the examples scripts forum, and other methods of using Chrome. Send is probably a last resort function to use, it's not very reliable.

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

Link to comment
Share on other sites

  • Moderators

That is a pretty vague question; it depends on which browser, and which control within the browser. 

Edit D'oh, too slow

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I used the wrong function name in my previous post, I meant to say use FileRreadToArray instead.

​It depends on which browser you're using, AutoIt comes with the Internet Explorer UDFs that allow you to use it with IE, there's a FF UDF in the examples scripts forum, and other methods of using Chrome. Send is probably a last resort function to use, it's not very reliable.

​so I could have avoided "a lot" of work using filereadtoarray? damn .. :D hmmm okay so I will search for something to work with chrome.. 

Edited by Siryx
Link to comment
Share on other sites

Siryx,

F.Y.I.  Please see comments in code...

#include <File.au3>
#include <Array.au3>

$file = "D:\Dokumente\Tracklist.txt"

#cs

FileOpen($file, 0)                      ; this stmt has no effect because there is no variable assigned to contain the file handle
                                        ; the stmt would normally be written as "local $hfl = fileopen($file,0)"

Global $arr[1000]
ReDim $arr[_FileCountLines($file)+1]    ; these two stmt could have been expressed as "Global $arr[_FileCountLines($file)+1]"

For $i = 1 to _FileCountLines($file)    ; _FileCountLines again, start of performance issues...should be ubound($arr) - 1
    $line = FileReadLine($file, $i)     ; performance hog - each FileReadLine causes an open/close sequence when using a file name, as opposed to a file handle
    $arr[$i] = $line
Next
#ce



local $arr = stringsplit(fileread($file),@CRLF,3)   ; read the entire file and split on EOL chars creating array $arr

                                                    ; this assumes @CRLF as the EOL sequence.  The last parm of stringsplit ("3") means
                                                    ;     use the entire delimiter string as the split criteria and do NOT return a count of lines in offset 0

                                                    ;   I might have written the above nested expression as:

                                                    ;       local $sFile = fileread($file)
                                                    ;       local $arr = stringsplit($sFile,@CRLF,3)

_ArraySort($arr)
_ArrayDisplay($arr)

kylomas 

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

oh well.. so basically I made it overly complicated, and it's even more compressed than before.. Isn't stringsplit supposed to make arr[0] = UBound($arr)?

Edited by Siryx
Link to comment
Share on other sites

About stringsplit: yes, StringSplit sets the 0'th element to the total count of split elements by default. (But you can change that by supplying $STR_NOCOUNT as the third parameter, see StringSplit helpfile: https://www.autoitscript.com/autoit3/docs/functions/StringSplit.htm.)

About integration in scite: yes, you can. It's a bit of a hassle IMHO, but certainly well worth it though if you often use the UDF in question. See here: https://www.autoitscript.com/wiki/Adding_UDFs_to_AutoIt_and_SciTE

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

About stringsplit: yes, StringSplit sets the 0'th element to the total count of split elements by default. (But you can change that by supplying $STR_NOCOUNT as the third parameter, see StringSplit helpfile: https://www.autoitscript.com/autoit3/docs/functions/StringSplit.htm.)

About integration in scite: yes, you can. It's a bit of a hassle IMHO, but certainly well worth it though if you often use the UDF in question. See here: https://www.autoitscript.com/wiki/Adding_UDFs_to_AutoIt_and_SciTE

​I didnt saw that 3 = 2+1 which means both flags are active .. but it seems like I can't change it to 1 bcuz it will fk the script up in a way i dont understand, lol. well better just use ubound i guess

Edited by Siryx
Link to comment
Share on other sites

now I got it, using 1 instead of 3 would sort $arr[0] to somewhere else. $arr[0] is then sth else and it fucks up because the amount of entrys in the array is now a string (in my case some songname starting with "!!!" .. :D

Edited by Siryx
Link to comment
Share on other sites

What? 
If there is a question in there, could you rephrase it?
Also, please read the helpfile for StringSplit to which I linked earlier (or access it from SciTE). It explains what flags 0, 1 and 2 (or rather $STR_CHRSPLIT, $STR_ENTIRESPLIT and $STR_NOCOUNT) do and it also holds an important note about splitting on @CRLF.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
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
 Share

  • Recently Browsing   0 members

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