Jump to content

FileReadToArray > element to variable


Recommended Posts

CODE
Dim $aRecords

If Not _FileReadToArray("hansmann2.txt",$aRecords) Then

MsgBox(4096,"Error", " Error reading log to Array error:" & @error)

Exit

EndIf

For $x = 1 to 5

ConsoleWrite($aRecords[$x] & @CRLF)

Next

Successfully displays first five records as:

D11,17,97, SAF IRA Janet 62727373, NBuy, YEuroPacific Growth, I28.070000, Q41.002000, T1,150.926140

D05,13,99, SAF IRA Janet 62727373, NBuy, YEuroPacific Growth, I32.809475, Q30.479000, T1,000.000000

D12,12,97, SAF IRA Janet 62727373, NReinvDiv, YEuroPacific Growth, I25.390000, Q0.581000, T14.751590

D12,12,97, SAF IRA Janet 62727373, NReinvLg, YEuroPacific Growth, I25.390000, Q1.332000, T33.819480

D12,12,97, SAF IRA Janet 62727373, NReinvSh, YEuroPacific Growth, I25.390000, Q0.364000, T9.241960

How do I set the 7th element to $var array? ie. $var = $aRecords[1][6]

I basically need to parse the array elements and assign them to variables.

I sincerely appreciate any and all help!

Thanks.

Link to comment
Share on other sites

CODE
Dim $aRecords

If Not _FileReadToArray("hansmann2.txt",$aRecords) Then

MsgBox(4096,"Error", " Error reading log to Array error:" & @error)

Exit

EndIf

For $x = 1 to 5

ConsoleWrite($aRecords[$x] & @CRLF)

Next

Successfully displays first five records as:

D11,17,97, SAF IRA Janet 62727373, NBuy, YEuroPacific Growth, I28.070000, Q41.002000, T1,150.926140

D05,13,99, SAF IRA Janet 62727373, NBuy, YEuroPacific Growth, I32.809475, Q30.479000, T1,000.000000

D12,12,97, SAF IRA Janet 62727373, NReinvDiv, YEuroPacific Growth, I25.390000, Q0.581000, T14.751590

D12,12,97, SAF IRA Janet 62727373, NReinvLg, YEuroPacific Growth, I25.390000, Q1.332000, T33.819480

D12,12,97, SAF IRA Janet 62727373, NReinvSh, YEuroPacific Growth, I25.390000, Q0.364000, T9.241960

How do I set the 7th element to $var array? ie. $var = $aRecords[1][6]

I basically need to parse the array elements and assign them to variables.

I sincerely appreciate any and all help!

Thanks.

_FileReadToArray() returns a 1D array. To break each line into an array of its own, use StringSplit():

Dim $aRecords 
If Not _FileReadToArray("hansmann2.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to 5
    $aSplit = StringSplit($aRecords[$x], ",")
    ConsoleWrite("Debug: " & $x & ": Seventh field = " & $aSplit[7] & @LF)
Next

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Are you trying to convert the entire array to a 2 dim array or just the one element?

To confirm my understanding:

  • this is a 2D array called $aRecords
  • each row in my output (5 above) is one record in the array
  • each record is made of elements
Maybe I have incorrectly assumed that this is a 2D and it's only a 1D (simply separated by @CRLF) in which case, "yes" I think I need to convert to a 2D, then call individual elements as variables.

Hope this makes sense... if not, I will try and describe in other words.

Gracious for the help.

Andrew

Link to comment
Share on other sites

$aRecords = "D11,17,97, SAF IRA Janet 62727373, NBuy, YEuroPacific Growth, I28.070000, Q41.002000, T1,150.926140"
$aRecords2 = StringReplace($aRecords, " ", "")
$aRecords3 = StringSplit($aRecords2, ",")
MsgBox( 0, "Test", $aRecords3[1])

Link to comment
Share on other sites

Successfully displays first five records as:

D11,17,97, SAF IRA Janet 62727373, NBuy, YEuroPacific Growth, I28.070000, Q41.002000, T1,150.926140

D05,13,99, SAF IRA Janet 62727373, NBuy, YEuroPacific Growth, I32.809475, Q30.479000, T1,000.000000

D12,12,97, SAF IRA Janet 62727373, NReinvDiv, YEuroPacific Growth, I25.390000, Q0.581000, T14.751590

D12,12,97, SAF IRA Janet 62727373, NReinvLg, YEuroPacific Growth, I25.390000, Q1.332000, T33.819480

D12,12,97, SAF IRA Janet 62727373, NReinvSh, YEuroPacific Growth, I25.390000, Q0.364000, T9.241960

Is "T1,150.926140" one or two elements? If one element, then you'll run into a problem with StringStrip() with "," delimiter.

Also, can you show here what kind of a result you want?

Example:

$item_id = "D11"

$item_code = "17"

etc.

This will help a lot.

Here's a long version that I come up with, hopefully someone here can come up with something more attractive. :)

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

Dim $aRecords

If Not _FileReadToArray("hansmann2.txt",$aRecords) Then
    MsgBox(4096,"Error", " Error reading log to Array error:" & @error)
    Exit
EndIf
_ArrayDisplay($aRecords, "$aRecords")

$record_1 = StringSplit($aRecords[1], ",")
_ArrayDisplay($record_1, "$record_1")
$record_2 = StringSplit($aRecords[2], ",")
_ArrayDisplay($record_2, "$record_2")
$record_3 = StringSplit($aRecords[3], ",")
_ArrayDisplay($record_3, "$record_3")
$record_4 = StringSplit($aRecords[4], ",")
_ArrayDisplay($record_4, "$record_4")
$record_5 = StringSplit($aRecords[5], ",")
_ArrayDisplay($record_5, "$record_5")

$element_1 = _ArrayCreate($record_1[1], $record_2[1], $record_3[1], $record_4[1], $record_5[1])
_ArrayDisplay($element_1, "$element_1")
$element_2 = _ArrayCreate($record_1[2], $record_2[2], $record_3[2], $record_4[2], $record_5[2])
_ArrayDisplay($element_2, "$element_2")
$element_3 = _ArrayCreate($record_1[3], $record_2[3], $record_3[3], $record_4[3], $record_5[3])
_ArrayDisplay($element_3, "$element_3")
$element_4 = _ArrayCreate($record_1[4], $record_2[4], $record_3[4], $record_4[4], $record_5[4])
_ArrayDisplay($element_4, "$element_4")
$element_5 = _ArrayCreate($record_1[5], $record_2[5], $record_3[5], $record_4[5], $record_5[5])
_ArrayDisplay($element_5, "$element_5")
$element_6 = _ArrayCreate($record_1[6], $record_2[6], $record_3[6], $record_4[6], $record_5[6])
_ArrayDisplay($element_6, "$element_6")
$element_7 = _ArrayCreate($record_1[7], $record_2[7], $record_3[7], $record_4[7], $record_5[7])
_ArrayDisplay($element_7, "$element_7")
$element_8 = _ArrayCreate($record_1[8], $record_2[8], $record_3[8], $record_4[8], $record_5[8])
_ArrayDisplay($element_8, "$element_8")
$element_9 = _ArrayCreate($record_1[9], $record_2[9], $record_3[9], $record_4[9], $record_5[9])
_ArrayDisplay($element_9, "$element_9")

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Link to comment
Share on other sites

Something I wrote a long time ago (hopefully it doesn't contain errors), thought it might help.

Func ConvertArrayTo2Dimen($Array, $Seperator)   ;returns new array  ;pass a single dim array return a 2dim array
    ;if not 1 dimen array then error =1 array is passed back
    Local $ColumnCount, $SplitLine

    If UBound($Array, 0) <> 1 Then
        SetError(1)
        Return $Array
    EndIf

    $ColumnCount = ''
    For $i = 1 To $Array[0]
        $SplitLine = StringSplit($Array[$i], $Seperator, 1)
        If $ColumnCount = '' Then
            $ColumnCount = $SplitLine[0]
        Else
            If $SplitLine[0] > $ColumnCount Then $ColumnCount = $SplitLine[0]
        EndIf
        $SplitLine = 0
    Next
    
    Dim $2DimenArray[$Array[0] + 1][$ColumnCount + 1]
    $2DimenArray[0][0] = $Array[0]
    
    For $i = 1 To $Array[0]
        $SplitLine = StringSplit($Array[$i], $Seperator, 1)
        For $k = 0 To $SplitLine[0]     ;find the value of [0] here in case not all are same column count
            $2DimenArray[$i][$k] = $SplitLine[$k]
        Next
    Next
    $Array = 0
    Return $2DimenArray
EndFunc   ;==>ConvertArrayTo2Dimen
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...