Jump to content

I Got this code a while back can someone help me know what the lines do?


RAMMRODD
 Share

Recommended Posts

I got this code from a post I asked about and it works great, but not sure how it works and I wish to learn how it does. I guess the hardest part for me to understand is how it knows to only copy partial parts of the input file. Here it is...

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

Dim $fileArray
Dim $output = ""

;Read text file to array
_FileReadToArray("test.txt", $fileArray)

;Loop through entire array
For $X = 1 to $fileArray[0]
   
   ;Copy each line into temporary buffer.
    $output &= $fileArray[$X] & @CRLF
   
   ;Look for score line StringInStr looks for a string in the whole string? Right?
    If StringInStr($fileArray[$X],"Marks for this submission:") Then

   ;Looks at the $temparray and splits it before the ":" or after ":" ?
        $tempArray = StringSplit($fileArray[$X], ":")

    ;sets $score to trim the string's right and left sides? Right? whats the "[2]" mean?
        $score = StringTrimRight(StringTrimLeft($tempArray[2], 1), 1)

     ;if the $score ="1/1" then copies it ok, but does this copy all the way up to the first line or how does it know when to 
;stop?
        If $score = "1/1" Then
           ;Copy buffer to new file
            FileWrite("Right Answers.txt", $output)
        EndIf
    

       
       ;Clear buffer
        $output = ""       
    EndIf
Next

____________________________EXAMPLE OF INPUT______________________________________________-

59

Marks: 1

Polar oceans, both N and S, existed during the

Choose one answer.

a. Middle Silurian

b. Middle Ordovician

c. Late Cambrian

Incorrect

Marks for this submission: 0/1.

60

Marks: 1

Proto-Pangea is called

Choose one answer.

a. Iapetus

b. Rodinia

c. Laurentia

d. Gondwanaland

Incorrect

Marks for this submission: 1/1.

61

Marks: 1

Reconstruction of the continents during the Paleozoic is accomplished by combining all but which of the following data?

Choose one answer.

a. paleomagnetic

b. paleoclimatic

c. bio-geographic

d. tectonic

e. all are relevant

Incorrect

Marks for this submission: 0/1.

_____________________________________________________________________________________________

Thanks,

Chad

Link to comment
Share on other sites

Something like

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

Dim $fileArray
Dim $output = ""

;Read text file to array
_FileReadToArray("test.txt", $fileArray)

;Loop through entire array, $fileArray[0] total line count of test.txt
For $X = 1 to $fileArray[0]
   
   ;Add a "carriage return, line feed" to line $X and append to temporary buffer.
    $output &= $fileArray[$X] & @CRLF
   
   ;StringInStr looks for "Marks for this submission:" in line $X of array $fileArray
    If StringInStr($fileArray[$X],"Marks for this submission:") Then

        ;split at ":"
        $tempArray = StringSplit($fileArray[$X], ":")

        ;element 2 of array $tempArray which will be " X/X."
        ;remove space before and "." after so we have "X/X"
        $score = StringTrimRight(StringTrimLeft($tempArray[2], 1), 1)

        If $score = "1/1" Then  ;Only if $score = "1/1" then append buffer to output file.           
           ;Buffer includes line "Marks for this submission: x/x."
            FileWrite("Right Answers.txt", $output)
        EndIf
        ;Clear buffer
        $output = ""       
    EndIf
Next

Link to comment
Share on other sites

thanks!!!!!!!!!!!

Question does this script check the lines 1 by 1 until it finds "Marks for this submission"?

or maybe reads from the end of 1 1/1 to the next 1/1?

I know this line trims down left and right, but how does it know how far to trim?

$score = StringTrimRight(StringTrimLeft($tempArray[2], 1), 1)

Edited by RAMMRODD
Link to comment
Share on other sites

Question does this script check the lines 1 by 1 until it finds "Marks for this submission"?

yes

I know this line trims down left and right, but how does it know how far to trim?

$score = StringTrimRight(StringTrimLeft($tempArray[2], 1), 1)

the sequence ",1), 1)" determines the number of characters to trim for StringTrimRight and StringTrimLeft respectively.
Link to comment
Share on other sites

What tells it that once it finds and trims "Marks for this submission: 1/1." to copy from there up to "1. line"?

See the following code, note the comments that follow the semicolon ";"
If $score = "1/1" Then ;Only if $score = "1/1" then append buffer to output file.          
          ;Buffer includes line "Marks for this submission: x/x."
            FileWrite("Right Answers.txt", $output)
        EndIf
If you haven't already, install the full version of Scitie here it comes with some very useful tools.
Link to comment
Share on other sites

so does it

1. line xxcopythislinexx

line xxcopythislinexx

line xxcopythislinexx

Marks for this submission: 1/1. then sees the 1/1 so it writes it to the output

2. line xxcopythislinexx

line xxcopythislinexx

line xxcopythislinexx

Marks for this submission: 0/1. then see's that 1/1 isnt there and doesnt copy it to the output file? It deletes it from its memory and moves on?

Maybe I'm getting warmer?

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...