Sign in to follow this  
Followers 0
cmilanelo

Automatizando processos

13 posts in this topic

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



#2 ·  Posted (edited)

= ="

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

"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

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

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

#7 ·  Posted (edited)

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

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

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

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

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:

COMPND    omega_1
ATOM      1  N   GLU     1     -11.199  -8.013   3.130  1.00  0.00           N
ATOM      3  CA  GLU     1      -9.765  -7.658   2.932  1.00  0.00           C
ATOM      5  C   GLU     1      -9.541  -6.179   3.203  1.00  0.00           C
ATOM      6  O   GLU     1     -10.461  -5.401   3.447  1.00  0.00           O
ATOM      7  CB  GLU     1      -9.309  -8.085   1.531  1.00  0.00           C
ATOM     10  CG  GLU     1     -10.056  -7.409   0.379  1.00  0.00           C
ATOM     13  CD  GLU     1      -9.646  -7.876  -1.022  1.00  0.00           C
ATOM     14  OE1 GLU     1     -10.590  -7.865  -1.865  1.00  0.00           O
ATOM     15  OE2 GLU     1      -8.441  -8.216  -1.177  1.00  0.00           O
ATOM     11 1HG  GLU     1      -9.898  -6.324   0.419  1.00  0.00           H
ATOM     12 2HG  GLU     1     -11.133  -7.588   0.472  1.00  0.00           H
ATOM      8 1HB  GLU     1      -8.236  -7.881   1.426  1.00  0.00           H
ATOM      9 2HB  GLU     1      -9.420  -9.173   1.438  1.00  0.00           H
ATOM      4  HA  GLU     1      -9.219  -8.237   3.686  1.00  0.00           H
ATOM      2 1H   GLU     1     -11.453  -7.946   4.119  1.00  0.00           H
ATOM     16 2H   GLU     1     -11.380  -8.970   2.816  1.00  0.00           H
ATOM     17 3H   GLU     1     -13.804  -7.380   2.600  1.00  0.00           H
ATOM     18  N   HIS     2      -8.200  -5.830   3.145  1.00  0.00           N
ATOM     20  CA  HIS     2      -7.733  -4.482   3.372  1.00  0.00           C
ATOM     22  C   HIS     2      -6.515  -4.271   2.480  1.00  0.00           C
ATOM     23  O   HIS     2      -6.021  -5.167   1.799  1.00  0.00           O
ATOM     24  CB  HIS     2      -7.367  -4.235   4.840  1.00  0.00           C
ATOM     27  CG  HIS     2      -6.261  -5.140   5.326  1.00  0.00           C
ATOM     28  ND1 HIS     2      -6.521  -6.366   5.890  1.00  0.00           N
ATOM     30  CD2 HIS     2      -4.903  -4.986   5.328  1.00  0.00           C
ATOM     32  CE1 HIS     2      -5.369  -6.953   6.230  1.00  0.00           C
ATOM     34  NE2 HIS     2      -4.377  -6.123   5.893  1.00  0.00           N
ATOM     31  HD2 HIS     2      -4.215  -4.229   5.009  1.00  0.00           H
ATOM     33  HE1 HIS     2      -5.261  -7.920   6.693  1.00  0.00           H
ATOM     25 1HB  HIS     2      -8.252  -4.359   5.479  1.00  0.00           H
ATOM     26 2HB  HIS     2      -7.049  -3.194   4.987  1.00  0.00           H
ATOM     21  HA  HIS     2      -8.537  -3.813   3.045  1.00  0.00           H
ATOM     29  HD1 HIS     2      -7.444  -6.768   6.028  1.00  0.00           H
ATOM     35  HE2 HIS     2      -3.387  -6.306   6.034  1.00  0.00           H
ATOM     19  H   HIS     2      -7.504  -6.549   2.976  1.00  0.00           H
ATOM     36  N   ALA     3      -6.079  -2.953   2.498  1.00  0.00           N
ATOM     38  CA  ALA     3      -4.972  -2.472   1.703  1.00  0.00           C
ATOM     40  C   ALA     3      -3.692  -2.686   2.501  1.00  0.00           C
ATOM     41  O   ALA     3      -3.463  -2.117   3.566  1.00  0.00           O
ATOM     42  CB  ALA     3      -5.147  -1.006   1.340  1.00  0.00           C
ATOM     43 1HB  ALA     3      -5.177  -0.376   2.236  1.00  0.00           H
ATOM     44 2HB  ALA     3      -4.322  -0.651   0.714  1.00  0.00           H
ATOM     45 3HB  ALA     3      -6.083  -0.849   0.792  1.00  0.00           H
ATOM     39  HA  ALA     3      -4.944  -3.086   0.796  1.00  0.00           H
ATOM     37  H   ALA     3      -6.562  -2.294   3.101  1.00  0.00           H
ATOM     46  N   TRP     4      -2.818  -3.554   1.861  1.00  0.00           N
ATOM     48  CA  TRP     4      -1.517  -3.885   2.396  1.00  0.00           C
ATOM     50  C   TRP     4      -0.624  -2.776   1.900  1.00  0.00           C
ATOM     51  O   TRP     4      -0.808  -2.279   0.787  1.00  0.00           O
ATOM     52  CB  TRP     4      -1.007  -5.243   1.918  1.00  0.00           C
ATOM     55  CG  TRP     4      -1.893  -6.383   2.358  1.00  0.00           C
ATOM     56  CD1 TRP     4      -1.934  -6.928   3.620  1.00  0.00           C
ATOM     58  CD2 TRP     4      -2.833  -7.085   1.552  1.00  0.00           C
ATOM     59  NE1 TRP     4      -2.861  -7.935   3.614  1.00  0.00           N
ATOM     61  CE2 TRP     4      -3.426  -8.054   2.364  1.00  0.00           C
ATOM     62  CE3 TRP     4      -3.236  -6.989   0.205  1.00  0.00           C
ATOM     64  CZ2 TRP     4      -4.407  -8.935   1.895  1.00  0.00           C
ATOM     66  CZ3 TRP     4      -4.216  -7.864  -0.277  1.00  0.00           C
ATOM     68  CH2 TRP     4      -4.791  -8.821   0.556  1.00  0.00           C
ATOM     67  HZ3 TRP     4      -4.532  -7.794  -1.315  1.00  0.00           H
ATOM     69  HH2 TRP     4      -5.551  -9.491   0.161  1.00  0.00           H
ATOM     63  HE3 TRP     4      -2.797  -6.249  -0.457  1.00  0.00           H
ATOM     65  HZ2 TRP     4      -4.855  -9.680   2.544  1.00  0.00           H
ATOM     57  HD1 TRP     4      -1.387  -6.691   4.522  1.00  0.00           H
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     5       0.387  -2.390   2.766  1.00  0.00           N
ATOM     71  CA  PRO     5       1.234  -1.232   2.514  1.00  0.00           C
ATOM     73  C   PRO     5       2.271  -1.651   1.516  1.00  0.00           C
ATOM     74  O   PRO     5       2.644  -2.818   1.487  1.00  0.00           O
ATOM     75  CB  PRO     5       1.852  -0.902   3.868  1.00  0.00           C
ATOM     78  CG  PRO     5       1.905  -2.256   4.573  1.00  0.00           C
ATOM     81  CD  PRO     5       0.641  -2.951   4.091  1.00  0.00           C
ATOM     79 1HG  PRO     5       2.790  -2.814   4.244  1.00  0.00           H
ATOM     80 2HG  PRO     5       1.947  -2.168   5.662  1.00  0.00           H
ATOM     76 1HB  PRO     5       1.188  -0.231   4.426  1.00  0.00           H
ATOM     77 2HB  PRO     5       2.838  -0.433   3.799  1.00  0.00           H
ATOM     82 1HD  PRO     5       0.750  -4.037   4.040  1.00  0.00           H
ATOM     83 2HD  PRO     5      -0.215  -2.706   4.730  1.00  0.00           H
ATOM     72  HA  PRO     5       0.654  -0.405   2.094  1.00  0.00           H
ATOM     84  N   SER     6       2.713  -0.627   0.700  1.00  0.00           N
ATOM     86  CA  SER     6       3.711  -0.812  -0.329  1.00  0.00           C
ATOM     88  C   SER     6       4.623   0.423  -0.456  1.00  0.00           C
ATOM     89  O   SER     6       4.709   1.126   0.593  1.00  0.00           O
ATOM     90  CB  SER     6       3.052  -1.286  -1.622  1.00  0.00           C
ATOM     93  OG  SER     6       2.209  -0.280  -2.171  1.00  0.00           O
ATOM     95  OXT SER     6       5.190   0.588  -1.571  1.00  0.00           O
ATOM     91 1HB  SER     6       3.798  -1.560  -2.376  1.00  0.00           H
ATOM     92 2HB  SER     6       2.428  -2.166  -1.433  1.00  0.00           H
ATOM     87  HA  SER     6       4.358  -1.622   0.029  1.00  0.00           H
ATOM     85  H   SER     6       2.318   0.299   0.830  1.00  0.00           H
ATOM     94  HG  SER     6       2.773   0.489  -2.349  1.00  0.00           H
TER      95      SER     6 
END

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

#12 ·  Posted (edited)

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

#13 ·  Posted (edited)

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  
Followers 0