Sign in to follow this  
Followers 0
lafafmentvotre

Read in array

11 posts in this topic

#1 ·  Posted (edited)

Hi everybody and sorry for my english

I need to read a txt files who contains variable informations like this :

computer1,108838

computer2,108836

computer3,108867

computer4,108825

and use the two arguments for each line with a for..to...next (computer and number)

But in my array, i obtain this :

Posted Image

Who can help me to find different value for each lines

Thanks for help

Edited by lafafmentvotre

Share this post


Link to post
Share on other sites



There have been several CSV UDFs posted. Do a little searching. At least one of them was posted by SmOke_N.

To do it yourself, just read the file into an array with _FileReadToArray(), then loop through that array, splitting each line and putting the parts wherever you need them (a separate 2D array, for example).

:idea:


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

Share this post


Link to post
Share on other sites

Hello

thanks for response

How can i split each line in 2D array ?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi,

#include <file.au3>
;only needed for _arraydisplay
#include <array.au3>
Global $aritems
;read file into array
_FileReadToArray (@ScriptDir & "\mytxt.txt", $aritems)
;Dim 2 d result array
Dim $arresult [$aritems [0]] [2]
;loop over file array
For $i = 1 To $aritems [0]
    ; Split string by "," and disabling of result return
    $temp = StringSplit ($aritems [$i], ",", 2)
    $arresult [$i - 1] [0] = $temp [0]
    $arresult [$i - 1] [1] = $temp [1]
Next
_ArrayDisplay ($arresult)

;-))

Stefan

@edit: sitting in the corner donkey hat on my head ....

Edited by 99ojo

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Create the 2D array with as many rows are there are lines in the file ([0] from the first array) and as many columns as you are interested in.

Take each element from the first array (line from the file) and use StringSplit() to get that line broken into an array of parts. Copy the parts from the StringSplit array to your 2D array.

If you get stuck, post what you've got and you'll get more help.

:)

Edit: Bad Stefan, he wanted to do it himself. No soup for you! :idea:

Edited by PsaltyDS

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

Share this post


Link to post
Share on other sites

#include <Array.au3>



Global $aTXT[4]

$aTXT[0] = "computer1,108838"

$aTXT[1] = "computer2,108836"

$aTXT[2] = "computer3,108867"

$aTXT[3] = "computer4,108825"



Global $aRow, $iRows = UBound($aTXT), $iCols = 1, $aTable[1][1]

For $i=0 To UBound($aTXT) - 1

    $aRow = StringSplit($aTXT[$i], ",")

    If $aRow[0] > $iCols Then $iCols = $aRow[0]

    ReDim $aTable[$i+1][$iCols]

    For $j=1 To $aRow[0]

        $aTable[$i][$j-1] = $aRow[$j]

    Next

Next

_ArrayDisplay($aTable)

Share this post


Link to post
Share on other sites

Bad zorphnog. No soup for you either! :idea:

Besides, if his array comes from _FileReadToArray() then $aTXT[0] is a count. Also, the 2D array should only be declared once and not need ReDim, since he already knows how many rows (from $aTXT[0]) and columns are of interest.

:)


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

Share this post


Link to post
Share on other sites

Thanks all for response

PsaltyDS, i learn with the Stefan script and understand now how to work with array.

Thanks

Share this post


Link to post
Share on other sites

Do i get now soup?

:idea:

Share this post


Link to post
Share on other sites

Do i get now soup?

Bread and water.

(And you looked good in the donkey hat!)

:idea:


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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Bad zorphnog. No soup for you either! :idea:

Besides, if his array comes from _FileReadToArray() then $aTXT[0] is a count. Also, the 2D array should only be declared once and not need ReDim, since he already knows how many rows (from $aTXT[0]) and columns are of interest.

:(

When did you become the soup nazi? Had I known that we could be paid in soup for posting I would've started posting much more often. :)

Yes I know the ReDim's are not needed in this particular situation. My post was meant to be more of a teaching script on how to deal with variable length strings since a badly formed csv file could potentially cause out of bounds errors. I try not to assume anything when dealing with arrays. Although performance wise, the ReDimming should be frowned upon.

Perhaps this would be better:

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

Global $aTXT
_FileReadToArray(@ScriptDir & "\mytxt.txt", $aTXT)
Global $aRow, $iRows = UBound($aTXT), $iCols = 2, $aTable[$aTXT[0]][2]
For $i=1 To $aTXT[0]
    $aRow = StringSplit($aTXT[$i], ",")
    If $aRow[0] > $iCols Then
        $iCols = $aRow[0]
        ReDim $aTable[$aTXT[0]][$iCols]
    EndIf
    For $j=1 To $aRow[0]
        $aTable[$i-1][$j-1] = $aRow[$j]
    Next
Next
_ArrayDisplay($aTable)
Edited by zorphnog

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