Sign in to follow this  
Followers 0
Ritt

Populating an Array w/ FileReadLine

9 posts in this topic

I've made a script that captures the pixel color of a 7x7 square. 

Now what I'm trying to do is read this info from the text document, and compile that info into an array which has two columns. The first column will have the hex colour, and the second how many times that colour appears. I'm able to take all of the information and make a 1 column array, and I know the if statement I would use to disregard duplicate entries. However I'm not familiar enough with arrays to understand how to populate the second column.

Below is what I have so far. It's heavily based off a script from Melba23 in another thread '?do=embed' frameborder='0' data-embedContent>>

Also anyone can explain to me what LSL does it would be much appreciated.

 

To summarize what I'm trying to do imagine a notepad with the following:

Thank

Thank

You

you

Very

Much

I'm trying to produce an array that would look like this

[Thank, 2]

[You, 2]

[Very, 1]

[Much, 1]

Please see below for what I have so far.

Thank you for your time.

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

$FileLoc = "Txt.txt"
Local $File = FileOpen($FileLoc, 0)

If $File = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
LSL($File))
FileClose($File)

Func LSL($Filehandle)

  Local $i = 0
    While 1
        Local $line = FileReadLine($Filehandle)
        If @error = -1 Then ExitLoop
        $i += 1
    WEnd
     ConsoleWrite($i & @CRLF)
      Local $avArray[$i]

    For $n = 0 To UBound($avArray) - 1
    $Lines = FileReadLine($Filehandle, $n +1)
       $avArray[$n] = $Lines
    
 Next
 _ArrayDisplay($avArray, "$avArray")
 EndFunc

Exit

Once I figure out multiple column arrays I'd add a statement like:

If Lines = $avArray[$n] Then

   ConsoleWrite("entry exists" & @CRLF)

   $avArray[$n]  [column 2] = $avArray[$n]  [column 2]  <------ this is the thing I have no idea how to do, syntax wise.

   else

$avArray[$n] = $Lines

endif

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I think figured out how arrays work. I kept getting this error:

" Array variable has incorrect number of subscripts or subscript dimension range exceeded"

Here's what I did to grasp the concept of rows/columns.

#include <Array.au3>
$R = 3
$C = 2
Local $avArray[$R][$C]
For $I = 0 to 2

$avArray[$I][1] = $I
     Next
 _ArrayDisplay($avArray, "$avArray")

Am still very much wanting to know about LSL. I'll update thread again when I finish the script. If anyone wants to share their "two cents" on arrays or anything mentioned above. I'm always looking to learn more about this.

Thanks again
            
 

Edited by Ritt

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

 
Func LSL($Filehandle)

  Local $i = 1
dim $line[2]
    While 1
if $i >= ubound($line) then
redim $line[$i + 1]
        Local $line[$i] = FileReadLine($Filehandle,$i)
if $line[$i] = "" then
exitloop
endif
        If @error = -1 Then 
ExitLoop
endif
        $i += 1

    WEnd
     ConsoleWrite($i & @CRLF)
      _ArrayDisplay($line, "Array")

i think that looks lil better.   as far as 2d arrays this is the example i was shown

<Array.au3>
Local $aStrings[6]=["a","bc","def","ghij","klmno","p"]
Local $aStringsSplit[UBound($aStrings)][1]

For $i = 0 To UBound($aStringsSplit)-1
    $aTemp = StringSplit($aStrings[$i],"",2)
    If UBound($aTemp)>UBound($aStringsSplit,2) Then ReDim $aStringsSplit[UBound($aStringsSplit)][UBound($aTemp)]
    For $j = 0 To UBound($aTemp)-1
        $aStringsSplit[$i][$j] = $aTemp[$j]
    Next
Next
_ArrayDisplay($aStringsSplit)

if you stare at that till you understand it then you will be much better off.  took a lil bit but its worth the effort to understanding what is happening there.

Edited by markyrocks

Share this post


Link to post
Share on other sites

Try this script, it will run much faster. It only redims the array when it needs to. You only run through the file once, instead of twice. It uses FileReadLine without a line number which is much much faster than giving it a line to read from.

Also, your @error line for exiting the loop is in the wrong place and that script will never exit the While loop correctly, and the first blank line the script finds will cause it to stop processing the file.

#include <array.au3>
$file = FileOpen("File.au3")
$aArray = LSL($file)
_ArrayDisplay($aArray)
Func LSL($Filehandle)
    Local $i = 0
    Local $avArray[2]
    While 1
        $avArray[$i] = FileReadLine($Filehandle)
        If @error = -1 Then ExitLoop
        $i += 1
        If $i >= UBound($avArray) Then
            ReDim $avArray[UBound($avArray) * 100]
        EndIf
    WEnd
    ReDim $avArray[$i]
    Return $avArray
EndFunc   ;==>LSL

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

#5 ·  Posted (edited)

well dude gots lots of help now.  I understand that the redim as i have it eats up clock cycles but w/e.  as long as the txt file isn't a few thousand lines long i don't think its that big a deal.  Im still learning to so i don't expect to be perfect.  Im just happy that i "finished" my program that is a few hundred lines long and it actually does what its supposed to without errors and i tested it like 30 different ways.  I'm sure if i threw that code up here people would be all over me lol.  I'm a hack.

also in my version i'm assuming that a blank line is the end of the file.

Edited by markyrocks

Share this post


Link to post
Share on other sites

Just trying to show you a different way. Not to mention, as I said, your @error test was in the wrong place. @error is reset after every function is executed, so you have to test for an error condition directly after the function that might throw the error you want to test for.


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

Thank you both.

It's going to take me awhile to fully process the lines you guys provided, as I'm not well versed in the language.

I'm still not completely sure what LSL does in the line  $aArray = LSL($file)

also in

ReDim $avArray[UBound($avArray) * 100]  why is it *100

Share this post


Link to post
Share on other sites

Ritt,

To answer some of your questions:

LSL is a function defined in the code.  In this case it returns the contents of a file as an array and stores  it in array = $aArray.

Redim is used to increase the size of an array.  It is very slow so BrewmanNH made it 100 times bigger than it was before the Redim.

Lastly,

Think of arrays as follows:

A 1d array is a list of items referenced from 0

A 2D array resembles a spreadsheet and is referenced by [row][cell] value.

Now a solution to your origional question...

#include <array.au3>

; Read the test file to a string variable.  Contents are shown in the console.
local $str = fileread(@scriptdir & '\test10.txt')
ConsoleWrite('Text from tst file : ' & @lf & $str & @LF)

; Replace string variable with return value from function and display in console.
$str = _DeleteDuplicateLines($str)
ConsoleWrite('Text after return from remove duplicates function:' & @lf & $str & @LF)

; create a 1D array from output.  This will be used to populate a 2D array.
local $a1DArray = stringsplit($str,@crlf,3) ; see Help file for parms
_arraydisplay($a1DArray,'This is a 1D Array')

; define 2D array using the # of rows from the 1D array just created
local $a2Darray[ubound($a1DArray)][2]

; populate 2D array
;
; See the Help file for an explanation of the string functions.
;Net effect is that they seperate the data at the space for each column of the 2D array.

for $1 = 0 to ubound($a1DArray) - 1
    $a2Darray[$1][0] = stringleft($a1DArray[$1],stringinstr($a1DArray[$1],' '))
    $a2Darray[$1][1] = stringright($a1DArray[$1],stringlen($a1DArray[$1]) - stringinstr($a1DArray[$1],' ',0,-1))
Next

_arraydisplay($a2Darray,'This is a 2D Array')

func _DeleteDuplicateLines($str, $sVerbose = 0)

    ; technique and regexp patterns by >>>> Malkey <<<<
    ; Removes duplicate lines and returns a string of CRLF delimited entries for each line followed by a count of how many time the line occured

    local $tstr,$soutput

    While $str <> ""
        $tstr = StringRegExpReplace($str, "(?s)^(\V+).*$", "\1")
        $str = StringRegExpReplace($str, "(?i)" & $tstr & '\v*', "")    ;   added case insensitivity
        $iNum = @extended

        if $sVerbose then
            if $inum then ConsoleWrite(stringformat('%03i dupicates deleted for line = [%-100s]',$inum-1,$tstr) & @LF)
        endif

        $sOutput &= $tstr & "  " & $iNum & @CRLF
    WEnd

    return $soutput

endfunc

The code is a little complicated.  Don't worry about the function (_DeleteDuplicateLines) for now.  Get comfortable with the string functions and array handling.  Later you can dig into SRE (the Klingon looking notation in the function).

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

Share this post


Link to post
Share on other sites

I really appreciate everyone's input. Thank you very much for your help.

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