Jump to content

text to array


Recommended Posts

Hi, i've got a txt document with data in the format

text;text;number;number;number

I need to split this into 5 seperate arrays. So say i've got

nick1;User;0;0;1000

nick2;User;0;0;1000

nick3;User;0;0;1000

nick4;User;0;0;1000

i need 1 array with nicks, 1 with users etc.

I've managed to do this using

$LadderData=FileRead("Ladder.txt")
    $UserList = StringSplit ( StringStripCR ( $LadderData), @LF  )
    ;_ArrayDisplay ($UserList)
    For $i=1 to $UserList [0]
        $Row = StringSplit ( $UserList[$i], ";" )
        _ArrayAdd ($nick, $Row[1])
        _ArrayAdd ($group, $Row[2])
        _ArrayAdd ($wins, $Row[3])
        _ArrayAdd ($losses, $Row[4])
        _ArrayAdd ($points, $Row[5])
    Next

but it takes to long. could anyone think of a better way? thanks

Link to comment
Share on other sites

Try this.

#include <Array.au3>

;$LadderData=FileRead("Ladder.txt")
Local $LadderData = "nick1;User1;1a;1b;1000" & @CRLF & _
        "nick2;User;0;0;1000" & @CRLF & _
        "nick3;User;0;0;1000" & @CRLF & _
        "nick4;User4;4a;4b;1004"

$UserList = StringSplit(StringStripCR($LadderData), @LF)

For $i = 1 To $UserList[0]
    $Row = StringSplit($UserList[$i], ";")
    If $i = 1 Then
        Local $aNick[$Row[0] - 1]
        Local $aGroup[$Row[0] - 1]
        Local $aWins[$Row[0] - 1]
        Local $aLosses[$Row[0] - 1]
        Local $aPoints[$Row[0] - 1]
    EndIf

    $aNick[$i - 1] = $Row[1]
    $aGroup[$i - 1] = $Row[2]
    $aWins[$i - 1] = $Row[3]
    $aLosses[$i - 1] = $Row[4]
    $aPoints[$i - 1] = $Row[5]
Next

_ArrayDisplay($aNick)
_ArrayDisplay($aGroup)
_ArrayDisplay($aWins)
_ArrayDisplay($aLosses)
_ArrayDisplay($aPoints)

Or this.

#Include <Array.au3>

;$LadderData=FileRead("Ladder.txt")

local $LadderData = "nick1;User1;1a;1b;1000" & @CRLF & _
    "nick2;User;0;0;1000" & @CRLF & _
    "nick3;User;0;0;1000" & @CRLF & _
    "nick4;User4;4a;4b;1004"

local $aNick = StringRegExp($LadderData,"(?m)^([^;]+?);",3)
    _ArrayDisplay($aNick)

local $aGroup = StringRegExp($LadderData,"(?m)^(?:[^;]+?;)([^;]+?);",3)
    _ArrayDisplay($aGroup)

local $aWins = StringRegExp($LadderData,"(?m)^(?:[^;]+?;){2}([^;]+?);",3)
    _ArrayDisplay($aWins)

local $aLosses = StringRegExp($LadderData,"(?m)^(?:[^;]+?;){3}([^;]+?);",3)
    _ArrayDisplay($aLosses)

local $aPoints = StringRegExp($LadderData,"(?m)(?:.+;)([^;]+?)$",3)
    _ArrayDisplay($aPoints)
Link to comment
Share on other sites

_ArrayAdd is ReDim - ing the array everytime and that is slowing the process down.

Here is my take:

#include <array.au3>
Global $FinalArray
$str = "nick1;User;0;0;1000"&@LF&"nick2;User;0;0;1000"&@LF&"nick3;User;0;0;1000"&@LF&"nick4;User;0;0;1000"
$Nicks = StringSplit($str, @LF)
For $i = 1 To $Nicks[0]
    $temp = StringSplit($Nicks[$i], ";")
    If $i = 1 Then Dim $FinalArray[$Nicks[0]][$temp[0]]
    For $j = 1 To $temp[0]
        $FinalArray[$i-1][$j-1] = $temp[$j]
    Next
Next
_ArrayDisplay($FinalArray)

Well, you need to put in the error checking.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Try this.

#include <Array.au3>

;$LadderData=FileRead("Ladder.txt")
Local $LadderData = "nick1;User1;1a;1b;1000" & @CRLF & _
        "nick2;User;0;0;1000" & @CRLF & _
        "nick3;User;0;0;1000" & @CRLF & _
        "nick4;User4;4a;4b;1004"

$UserList = StringSplit(StringStripCR($LadderData), @LF)

For $i = 1 To $UserList[0]
    $Row = StringSplit($UserList[$i], ";")
    If $i = 1 Then
        Local $aNick[$Row[0] - 1]
        Local $aGroup[$Row[0] - 1]
        Local $aWins[$Row[0] - 1]
        Local $aLosses[$Row[0] - 1]
        Local $aPoints[$Row[0] - 1]
    EndIf

    $aNick[$i - 1] = $Row[1]
    $aGroup[$i - 1] = $Row[2]
    $aWins[$i - 1] = $Row[3]
    $aLosses[$i - 1] = $Row[4]
    $aPoints[$i - 1] = $Row[5]
Next

_ArrayDisplay($aNick)
_ArrayDisplay($aGroup)
_ArrayDisplay($aWins)
_ArrayDisplay($aLosses)
_ArrayDisplay($aPoints)

Or this.

#Include <Array.au3>

;$LadderData=FileRead("Ladder.txt")

local $LadderData = "nick1;User1;1a;1b;1000" & @CRLF & _
    "nick2;User;0;0;1000" & @CRLF & _
    "nick3;User;0;0;1000" & @CRLF & _
    "nick4;User4;4a;4b;1004"

local $aNick = StringRegExp($LadderData,"(?m)^([^;]+?);",3)
    _ArrayDisplay($aNick)

local $aGroup = StringRegExp($LadderData,"(?m)^(?:[^;]+?;)([^;]+?);",3)
    _ArrayDisplay($aGroup)

local $aWins = StringRegExp($LadderData,"(?m)^(?:[^;]+?;){2}([^;]+?);",3)
    _ArrayDisplay($aWins)

local $aLosses = StringRegExp($LadderData,"(?m)^(?:[^;]+?;){3}([^;]+?);",3)
    _ArrayDisplay($aLosses)

local $aPoints = StringRegExp($LadderData,"(?m)(?:.+;)([^;]+?)$",3)
    _ArrayDisplay($aPoints)

wow thanks! with 1000 items in UserList this converts it into 5 arrays in only 0.2 seconds instead of 1.6 (what i had)

although i do not quite understand what you've done there. I've read up on StringRepExp but it's not easy to understand how to set out the pattern :huh2:

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