Jump to content
Sign in to follow this  
cmilanelo

Automatizando processos

Recommended Posts

cmilanelo

Sorry for my bad English.

I'm new here and again using the AutoIt too. I'm using it to automate processes of different types and am having difficulty in one.

I'm working with files in. Txt files whose content is standardized d aseguinte form:

ATOM 53 1HB TRP 4 0,006 -5,420 2,304 1.00 0.00 H

ATOM 54 2HB TRP 4 -0,907 -5,255 0,824 1.00 0.00 H

ATOM 49 HA TRP 4 -1,577 -3,825 3,487 1.00 0.00 H

ATOM 60 HE1 TRP 4 -3,097 -8,510 4,411 1.00 0.00 H

ATOM 47 H TRP 4 -3,107 -4,011 1,001 1.00 0.00 H

ATOM 70 N PRO 0387 -2390 5 2766 1:00 0:00 N

ATOM 71 CA PRO 1234 -1232 5 2514 1:00 C 0:00

ATOM 73 C PRO 2271 -1651 5 1516 1:00 C 0:00

What I need to do is write a code that read the first three columns of decimal numbers separately and check which are the highest and lowest numbers in each column.

The files are located inside a folder and the code should generate another. Txt file with the results.

Can anyone help me?

Share this post


Link to post
Share on other sites
MiserableLife

= ="

read the help file....

it has every thing you need to know about autoit.

Learn what is @CR @LF and

look for StringSplit , this function should do the job.

Edited by MiserableLife

Share this post


Link to post
Share on other sites
Nurav

"ForumSupport.txt" should be placed under the same directory from which you are running the script. The text file should contain your formatted text with no space between lines.

#include <Array.au3>
$File = FileOpen("ForumSupport.txt", 0)
Dim $Array[1][2]
$i=0

While - 1
    $FileLine = StringReplace(FileReadLine($File), ",", ".")

    if $FileLine="" Then
        ExitLoop
    EndIf

    $String = StringSplit($FileLine, " ")

    $Num1 = Number($String[6])
    $Num2 = Number($String[7])
    $Num3 = Number($String[8])
    $largest = $Num1

    If $Num2 > $largest Then
        $largest = $Num2
    EndIf

    If $Num3 > $largest Then
        $largest = $Num3
    EndIf

    ReDim $Array[$i+1][4]
    $Array[$i][0]=$Num1
    $Array[$i][1]=$Num2
    $Array[$i][2]=$Num3
    $Array[$i][3]=$largest

$i=$i+1

WEnd
_ArrayDisplay($Array)

Share this post


Link to post
Share on other sites
Nurav

the last column of the displayed array will be the largest number.

Share this post


Link to post
Share on other sites
jchd

Please double check the format of your input file so that there is no misunderstanding. I replaced spaces by tabs to align column and made lines comments so that they aren't mangled thru posting.

#cs
ATOM    53  1HB TRP 4       0,006   -5,420  2,304   1.00    0.00    H
ATOM    54  2HB TRP 4       -0,907  -5,255  0,824   1.00    0.00    H
ATOM    49  HA  TRP 4       -1,577  -3,825  3,487   1.00    0.00    H
ATOM    60  HE1 TRP 4       -3,097  -8,510  4,411   1.00    0.00    H
ATOM    47  H   TRP 4       -3,107  -4,011  1,001   1.00    0.00    H
ATOM    70  N   PRO 0387    -2390   5       2766    1:00    0:00    N
ATOM    71  CA  PRO 1234    -1232   5       2514    1:00    C   0:00
ATOM    73  C   PRO 2271    -1651   5       1516    1:00    C   0:00
;___________________****____^^^^^^__^^^^^^__^^^^^   
#ce

Are you interested by the columns with ^^^ mark or does the column marked *** matter?

Edit: what makes me ask is the entry PRO 0387, with a leading zero.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
cmilanelo

Please double check the format of your input file so that there is no misunderstanding. I replaced spaces by tabs to align column and made lines comments so that they aren't mangled thru posting.

#cs
ATOM    53  1HB TRP 4       0,006   -5,420  2,304   1.00    0.00    H
ATOM    54  2HB TRP 4       -0,907  -5,255  0,824   1.00    0.00    H
ATOM    49  HA  TRP 4       -1,577  -3,825  3,487   1.00    0.00    H
ATOM    60  HE1 TRP 4       -3,097  -8,510  4,411   1.00    0.00    H
ATOM    47  H   TRP 4       -3,107  -4,011  1,001   1.00    0.00    H
ATOM    70  N   PRO 0387    -2390   5       2766    1:00    0:00    N
ATOM    71  CA  PRO 1234    -1232   5       2514    1:00    C   0:00
ATOM    73  C   PRO 2271    -1651   5       1516    1:00    C   0:00
;___________________****____^^^^^^__^^^^^^__^^^^^   
#ce

Are you interested by the columns with ^^^ mark or does the column marked *** matter?

Edit: what makes me ask is the entry PRO 0387, with a leading zero.

I had noticed that the data were posted wrong. The right way in which they find themselves is this:

ATOM    53  1HB TRP 4       0,006   -5,420  2,304   1.00    0.00    H
ATOM    54  2HB TRP 4       -0,907  -5,255  0,824   1.00    0.00    H
ATOM    49  HA  TRP 4       -1,577  -3,825  3,487   1.00    0.00    H
ATOM    60  HE1 TRP 4       -3,097  -8,510  4,411   1.00    0.00    H
ATOM    47  H   TRP 4       -3,107  -4,011  1,001   1.00    0.00    H

Share this post


Link to post
Share on other sites
jchd

That sounds much better. I assume your interest is in the 3 columns after the constant '4' so it's OK I guess.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Malkey

Try this.

#include <Array.au3>

#cs
Local $sFileIn = "FullPath & \FileName.ext"
If FileExists($sFileIn) Then
    Local $sData = FileRead($sFileIn)
Else
    MsgBox(4096, "Error", " File does not exist")
    Exit
EndIf
#ce

Local $sData = "ATOM    53 1HB TRP 4    0,006 -5,420 2,304 1.00 0.00    H" & @CRLF & _
                "ATOM   54 2HB TRP 4    -0,907 -5,255 0,824 1.00    0.00    H" & @CRLF & _
                "ATOM   49 HA TRP 4     -1,577 -3,825 3,487 1.00    0.00    H" & @CRLF & _
                "ATOM   60 HE1 TRP 4    -3,097 -8,510 4,411 1.00    0.00    H" & @CRLF & _
                "ATOM   47 H TRP 4  -3,107 -4,011 1,001 1.00    0.00    H"

Local $aMaxMin[3][4] = [[" ", "Col6", "Col7", "Col8"],["Max"],["Min"]]
Local $aTemp

For $j = 1 To 3
    $aTemp = _StringColumnMaxMin($sData, $j + 5)
    $aMaxMin[1][$j] = $aTemp[0]
    $aMaxMin[2][$j] = $aTemp[1]
Next

_ArrayDisplay($aMaxMin)


; Returns an array. array[0] contains Maximum; array[1] contains Minimum
Func _StringColumnMaxMin($sDataIn, $col = 1)
    Local $aArray, $sStr, $iMaxMin[2]

    $sStr = StringRegExpReplace($sDataIn & @CRLF, "(?:\h*?)(?:[^ ]+\h+?){" & $col - 1 & "}([0-9\-.,]+)(?:\h*.*)\v+|\z", "\1" & @CRLF)
    $aArray = StringSplit(StringStripWS($sStr, 2), @CRLF, 3)

    $iMaxMin[0] = $aArray[0] ;Max
    $iMaxMin[1] = $aArray[0] ;Min

    ; Numeric comparsion without thousand comma to find max and min values in array, $aArray.
    For $i = 1 To UBound($aArray)-1
        If (Number(StringReplace($aArray[$i],",","")) > Number(StringReplace($iMaxMin[0],",",""))) Then _
                $iMaxMin[0] = $aArray[$i] ; Max
        If (Number(StringReplace($aArray[$i],",","")) < Number(StringReplace($iMaxMin[1],",",""))) Then _
                $iMaxMin[1] = $aArray[$i] ; Min
    Next

    Return $iMaxMin
EndFunc ;==>_StringColumnMaxMin

Share this post


Link to post
Share on other sites
cmilanelo

That sounds much better. I assume your interest is in the 3 columns after the constant '4' so it's OK I guess.

Exactly. I need to know the highest and lowest value in each of the 3 columns listed after the '4 '.

Share this post


Link to post
Share on other sites
cmilanelo

Try this.

#include <Array.au3>

#cs
Local $sFileIn = "FullPath & \FileName.ext"
If FileExists($sFileIn) Then
    Local $sData = FileRead($sFileIn)
Else
    MsgBox(4096, "Error", " File does not exist")
    Exit
EndIf
#ce

Local $sData = "ATOM    53 1HB TRP 4    0,006 -5,420 2,304 1.00 0.00    H" & @CRLF & _
                "ATOM   54 2HB TRP 4    -0,907 -5,255 0,824 1.00    0.00    H" & @CRLF & _
                "ATOM   49 HA TRP 4     -1,577 -3,825 3,487 1.00    0.00    H" & @CRLF & _
                "ATOM   60 HE1 TRP 4    -3,097 -8,510 4,411 1.00    0.00    H" & @CRLF & _
                "ATOM   47 H TRP 4  -3,107 -4,011 1,001 1.00    0.00    H"

Local $aMaxMin[3][4] = [[" ", "Col6", "Col7", "Col8"],["Max"],["Min"]]
Local $aTemp

For $j = 1 To 3
    $aTemp = _StringColumnMaxMin($sData, $j + 5)
    $aMaxMin[1][$j] = $aTemp[0]
    $aMaxMin[2][$j] = $aTemp[1]
Next

_ArrayDisplay($aMaxMin)


; Returns an array. array[0] contains Maximum; array[1] contains Minimum
Func _StringColumnMaxMin($sDataIn, $col = 1)
    Local $aArray, $sStr, $iMaxMin[2]

    $sStr = StringRegExpReplace($sDataIn & @CRLF, "(?:\h*?)(?:[^ ]+\h+?){" & $col - 1 & "}([0-9\-.,]+)(?:\h*.*)\v+|\z", "\1" & @CRLF)
    $aArray = StringSplit(StringStripWS($sStr, 2), @CRLF, 3)

    $iMaxMin[0] = $aArray[0] ;Max
    $iMaxMin[1] = $aArray[0] ;Min

    ; Numeric comparsion without thousand comma to find max and min values in array, $aArray.
    For $i = 1 To UBound($aArray)-1
        If (Number(StringReplace($aArray[$i],",","")) > Number(StringReplace($iMaxMin[0],",",""))) Then _
                $iMaxMin[0] = $aArray[$i] ; Max
        If (Number(StringReplace($aArray[$i],",","")) < Number(StringReplace($iMaxMin[1],",",""))) Then _
                $iMaxMin[1] = $aArray[$i] ; Min
    Next

    Return $iMaxMin
EndFunc ;==>_StringColumnMaxMin
It works perfectly! Thank you VERY much!

Share this post


Link to post
Share on other sites
cmilanelo

Try this.

#include <Array.au3>

#cs
Local $sFileIn = "FullPath & \FileName.ext"
If FileExists($sFileIn) Then
    Local $sData = FileRead($sFileIn)
Else
    MsgBox(4096, "Error", " File does not exist")
    Exit
EndIf
#ce

Local $sData = "ATOM    53 1HB TRP 4    0,006 -5,420 2,304 1.00 0.00    H" & @CRLF & _
                "ATOM   54 2HB TRP 4    -0,907 -5,255 0,824 1.00    0.00    H" & @CRLF & _
                "ATOM   49 HA TRP 4     -1,577 -3,825 3,487 1.00    0.00    H" & @CRLF & _
                "ATOM   60 HE1 TRP 4    -3,097 -8,510 4,411 1.00    0.00    H" & @CRLF & _
                "ATOM   47 H TRP 4  -3,107 -4,011 1,001 1.00    0.00    H"

Local $aMaxMin[3][4] = [[" ", "Col6", "Col7", "Col8"],["Max"],["Min"]]
Local $aTemp

For $j = 1 To 3
    $aTemp = _StringColumnMaxMin($sData, $j + 5)
    $aMaxMin[1][$j] = $aTemp[0]
    $aMaxMin[2][$j] = $aTemp[1]
Next

_ArrayDisplay($aMaxMin)


; Returns an array. array[0] contains Maximum; array[1] contains Minimum
Func _StringColumnMaxMin($sDataIn, $col = 1)
    Local $aArray, $sStr, $iMaxMin[2]

    $sStr = StringRegExpReplace($sDataIn & @CRLF, "(?:\h*?)(?:[^ ]+\h+?){" & $col - 1 & "}([0-9\-.,]+)(?:\h*.*)\v+|\z", "\1" & @CRLF)
    $aArray = StringSplit(StringStripWS($sStr, 2), @CRLF, 3)

    $iMaxMin[0] = $aArray[0] ;Max
    $iMaxMin[1] = $aArray[0] ;Min

    ; Numeric comparsion without thousand comma to find max and min values in array, $aArray.
    For $i = 1 To UBound($aArray)-1
        If (Number(StringReplace($aArray[$i],",","")) > Number(StringReplace($iMaxMin[0],",",""))) Then _
                $iMaxMin[0] = $aArray[$i] ; Max
        If (Number(StringReplace($aArray[$i],",","")) < Number(StringReplace($iMaxMin[1],",",""))) Then _
                $iMaxMin[1] = $aArray[$i] ; Min
    Next

    Return $iMaxMin
EndFunc ;==>_StringColumnMaxMin
Malkey, I noticed right now that's an error in the code. :(

In the falow text:

 

The code send me the falow result:

[0]|   |   Col6|   Col7| Col8
[1]|Max|  5.190|  1.126| 6.693
[2]|Min|-13.804|-11.199|-8.013

So, it said to me in the col7, the min number is the first number of the col6 and the min number of the col8 is the first of the col7. However, the max numbers are right in all of them. What's happening? :)

Thank you man!

Share this post


Link to post
Share on other sites
Malkey

I believe the problem occurred because of the lines at the beginning and at the end of the data that did not have 6, 7,or 8 columns to be read.

The fix involved keeping the lines of the data which have greater than 8 columns. And, discarding the rest.

Here is the result from the data @ Post #11

[0]|...|.. Col6|. Col7| Col8
[1]|Max|. 5.190| 1.126| 6.693
[2]|Min|-13.804|-9.680|-2.376

#include <Array.au3>

; Data from http://www.autoitscript.com/forum/index.php?showtopic=112577&view=findpost&p=789740
; used in file "Test1.txt" in script directory.
Local $sFileIn = "Test1.txt"
If FileExists($sFileIn) Then
    Local $sData = FileRead($sFileIn)
Else
    MsgBox(4096, "Error", " File does not exist")
    Exit
EndIf

#cs
    Local $sData = "ATOM    53 1HB TRP 4    0,006 -5,420 2,304 1.00 0.00    H" & @CRLF & _
    "ATOM 54 2HB TRP 4  -0,907 -5,255 0,824 1.00    0.00    H" & @CRLF & _
    "ATOM 49 HA TRP 4   -1,577 -3,825 3,487 1.00    0.00    H" & @CRLF & _
    "ATOM 60 HE1 TRP 4  -3,097 -8,510 4,411 1.00    0.00    H" & @CRLF & _
    "ATOM 47 H TRP 4 -3,107 -4,011 1,001 1.00   0.00    H"
#ce
Local $aMaxMin[3][4] = [[" ", "Col6", "Col7", "Col8"],["Max"],["Min"]]
Local $aTemp

For $j = 1 To 3
    $aTemp = _StringColumnMaxMin($sData, $j + 5)
    $aMaxMin[1][$j] = $aTemp[0]
    $aMaxMin[2][$j] = $aTemp[1]
Next

_ArrayDisplay($aMaxMin)


; Returns an array. array[0] contains Maximum; array[1] contains Minimum
Func _StringColumnMaxMin($sDataIn, $col = 1)
    Local $aArray, $sStr, $iMaxMin[2], $iNoLines, $sDataInModified, $sLine

    ; Check if columns 6, 7, & 8, exist on every line, else remove that line.
    StringRegExpReplace(StringStripWS($sDataIn, 3) & @CRLF, @CRLF, @CRLF)
    $iNoLines = @extended
    For $i = 1 To $iNoLines
        $sLine = StringRegExpReplace($sDataIn, "^(.*\v+?|$){" & $i & "}(?s).*", "\1") ; Return line no. 1-based
        StringRegExpReplace($sLine, "(?:[^ ]+?)(\h+?)", "")
        If @extended > 8 Then $sDataInModified &= $sLine ; Check number of "word-spaces" occurrences on each line.
    Next

    $sStr = StringRegExpReplace($sDataInModified & @CRLF, "(?:\h*?)(?:[^ ]+\h+?){" & $col - 1 & "}([0-9\-.,]+)(?:\h*.*)\v+|\z", "\1" & @CRLF)
    $aArray = StringSplit(StringStripWS($sStr, 2), @CRLF, 3)
    $iMaxMin[0] = $aArray[0] ;Max
    $iMaxMin[1] = $aArray[0] ;Min

    ; Numeric comparsion without thousand comma to find max and min values in array, $aArray.
    For $i = 1 To UBound($aArray) - 1
        If (Number(StringReplace($aArray[$i], ",", "")) > Number(StringReplace($iMaxMin[0], ",", ""))) Then _
                $iMaxMin[0] = $aArray[$i] ; Max
        If (Number(StringReplace($aArray[$i], ",", "")) < Number(StringReplace($iMaxMin[1], ",", ""))) Then _
                $iMaxMin[1] = $aArray[$i] ; Min
    Next
    Return $iMaxMin
EndFunc ;==>_StringColumnMaxMin

Edit:

This next script does the same thing. I believe it is simpler, easier to understand, and faster.

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

; Data from http://www.autoitscript.com/forum/index.php?showtopic=112577&view=findpost&p=789740
; put in file "Test1.txt" in script directory.
Local $sFileIn = "Test1.txt"

Local $aMaxMin[3][4] = [[" ", "Col6", "Col7", "Col8"],["Max"],["Min"]]
Local $aTemp

For $j = 1 To 3
    $aTemp = _StringColumnMaxMin($sFileIn, $j + 5)
    $aMaxMin[1][$j] = $aTemp[0]
    $aMaxMin[2][$j] = $aTemp[1]
Next

_ArrayDisplay($aMaxMin)


; Returns an array. array[0] contains Maximum; array[1] contains Minimum
Func _StringColumnMaxMin($sFileIn, $col = 1)
    Local $aArray, $sStr, $iMaxMin[2], $sLine, $aFile, $sDataInModified
    _FileReadToArray($sFileIn, $aFile)

    For $i = 1 To $aFile[0]
        StringRegExpReplace($aFile[$i], "(?:[^ ]+?)(\h+?)", "")
        If @extended > 8 Then $sDataInModified &= $aFile[$i] & @CRLF; Check number of "word-spaces" occurrences on each line.
    Next

    $sStr = StringRegExpReplace($sDataInModified & @CRLF, "(?:\h*?)(?:[^ ]+\h+?){" & $col - 1 & "}([0-9\-.,]+)(?:\h*.*)\v+|\z", "\1" & @CRLF)
    $aArray = StringSplit(StringStripWS($sStr, 2), @CRLF, 3)
    $iMaxMin[0] = $aArray[0] ;Max
    $iMaxMin[1] = $aArray[0] ;Min

    ; Numeric comparsion without thousand comma to find max and min values in array, $aArray.
    For $i = 1 To UBound($aArray) - 1
        If (Number(StringReplace($aArray[$i], ",", "")) > Number(StringReplace($iMaxMin[0], ",", ""))) Then _
                $iMaxMin[0] = $aArray[$i] ; Max
        If (Number(StringReplace($aArray[$i], ",", "")) < Number(StringReplace($iMaxMin[1], ",", ""))) Then _
                $iMaxMin[1] = $aArray[$i] ; Min
    Next
    Return $iMaxMin
EndFunc ;==>_StringColumnMaxMin
Edited by Malkey

Share this post


Link to post
Share on other sites
Nurav

Before I got your requirement wrong. Here you have another way to do things.

#Include <String.au3>
#Include <File.au3>
#include <Array.au3>
$File = FileOpen("Test1.txt", 0)
Dim $Col6Array[1]
Dim $Col7Array[1]
Dim $Col8Array[1]


While - 1
    $FileLine = StringReplace(FileReadLine($File), ",", ".")

    while StringInStr($FileLine," ")
    $FileLine = StringReplace($FileLine, " ", "_")
    WEnd

    while StringInStr($FileLine,"__")
    $FileLine = StringReplace($FileLine, "__", "_")
    WEnd

    $String = StringSplit($FileLine, "_")


    if ($FileLine<>"") And ($String[0]=11) Then

        $Num1 = Number($String[6])
        $Num2 = Number($String[7])
        $Num3 = Number($String[8])
        _ArrayAdd($Col6Array,$Num1)
        _ArrayAdd($Col7Array,$Num2)
        _ArrayAdd($Col8Array,$Num3)

    ElseIf $FileLine="" Then
        ExitLoop

    EndIf

WEnd

MsgBox("",""," Col6 MIN="&_ArrayMin($Col6Array)&" Col6 MAX="&_ArrayMax($Col6Array)&@CRLF _
            &" Col7 MIN="&_ArrayMin($Col7Array)&" Col7 MAX="&_ArrayMax($Col7Array)&@CRLF _
            &" Col8 MIN="&_ArrayMin($Col8Array)&" Col8 MAX="&_ArrayMax($Col8Array))
Edited by Nurav

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  

×