Jump to content

Split String To Array


Recommended Posts

kylomas,

You don't need to escape | in a character class.

 

Thanks, good to know...non the less it works...

edit: although I think I like UEZ's solution better...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

It's unforgiving, though it shouldn't be. Help file denotes which should be escaped and which special characters retaining their meaning. R doesn't which I found out the other day!

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

 

here ya go...if you want an array for each line

#include <array.au3>
$string = "Info1||||Info2||||Info3||||Info4" & @CRLF & _
"Info5||||Info6||||Info7||||Info8" & @CRLF & _
"Info9||||Info10||||Info11||||Info12"
ConsoleWrite($string)

$aCRLFSplit = StringSplit($string,@CRLF,3)
For $i = 0 To UBound($aCRLFSplit)-1
    $aCRLFSplit[$i] = StringSplit($aCRLFSplit[$i],"||||",3)
    _ArrayDisplay($aCRLFSplit[$i])
Next

Nearly ;) I got it working when replacing @CRLF with @LF :P

Otherwise if you use @CRLF this will happen:

4csa6pce.png

Link to comment
Share on other sites

interesting, I can't reproduce that...i get an array of three arrays, each with 4 values

edited to output the arrays:

#include <array.au3>

$string = "Info1||||Info2||||Info3||||Info4" & @CRLF & _
"Info5||||Info6||||Info7||||Info8" & @CRLF & _
"Info9||||Info10||||Info11||||Info12"
;~ ConsoleWrite($string & @CRLF)

$aCRLFSplit = StringSplit($string,@CRLF,3)
For $i = 0 To UBound($aCRLFSplit)-1
    $aCRLFSplit[$i] = StringSplit($aCRLFSplit[$i],"||||",3)
    ConsoleWrite(_ArrayToString($aCRLFSplit[$i]) & @CRLF)
Next

where output is:

Info1|Info2|Info3|Info4
Info5|Info6|Info7|Info8
Info9|Info10|Info11|Info12

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Is it possible to add the splitted items in a listview, so i can view it more clear?

Like Info1 is the Itemtext and for subitem 1 i get the first split which is info2 etc etc.

And by @LF (Linefeed) I create a new ListViewItem, and also split into the subitems by ||| ?

Because the console writeline is not that clear.

Also I like to visualize my splitted stuff in a gui app (No Array_Display)

Link to comment
Share on other sites

not sure, exactly what you need...added 2 loops to create a 2d array, and to ouput through the array of arrays

#include <array.au3>

$string = "Info1||||Info2||||Info3||||Info4" & @CRLF & _
"Info5||||Info6||||Info7||||Info8" & @CRLF & _
"Info9||||Info10||||Info11||||Info12"
;~ ConsoleWrite($string & @CRLF)

$aCRLFSplit = StringSplit($string,@CRLF,3)
For $i = 0 To UBound($aCRLFSplit)-1
    $aCRLFSplit[$i] = StringSplit($aCRLFSplit[$i],"||||",3)
;~  ConsoleWrite(_ArrayToString($aCRLFSplit[$i]) & @CRLF)
Next

For $i = 0 To UBound($aCRLFSplit)-1
    $aTemp = $aCRLFSplit[$i]
    For $j = 0 To UBound($aTemp)-1
        ConsoleWrite("Array[" & $i & "]; SubArray[" & $j & "] value=[" & $aTemp[$j] & "]." & @CRLF)
    Next
Next

; sample of creating a 2d array
Local $2DArray[UBound($aCRLFSplit)][1]
For $i = 0 To UBound($aCRLFSplit)-1
    $aTemp = $aCRLFSplit[$i]
    If UBound($aTemp) > UBound($2DArray,2) Then
        ReDim $2DArray[UBound($aCRLFSplit)][UBound($aTemp)]
    EndIf
    For $j = 0 To UBound($aTemp)-1
        $2DArray[$i][$j] = $aTemp[$j]
    Next
Next
_ArrayDisplay($2DArray)
; sample of creating a transposed 2d array
Local $2DArrayTransposed[1][UBound($aCRLFSplit)]
For $i = 0 To UBound($aCRLFSplit)-1
 $aTemp = $aCRLFSplit[$i]
 If UBound($aTemp) > UBound($2DArrayTransposed) Then
  ReDim $2DArrayTransposed[UBound($aTemp)][UBound($aCRLFSplit)]
 EndIf
 For $j = 0 To UBound($aTemp)-1
  $2DArrayTransposed[$j][$i] = $aTemp[$j]
 Next
Next
_ArrayDisplay($2DArrayTransposed)











added another case for a transposed 2d array

gui demonstration:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <array.au3>

$string = "Info1||||Info2||||Info3||||Info4" & @CRLF & _
"Info5||||Info6||||Info7||||Info8" & @CRLF & _
"Info9||||Info10||||Info11||||Info12"
;~ ConsoleWrite($string & @CRLF)

$iGuiWidth = 200
$iControlHeight = 20
$iControlBuffer = 10
$iControlWidth  = $iGuiWidth-$iControlBuffer*2

$aCRLFSplit = StringSplit($string,@CRLF,3)
$iGuiHeight = ((UBound($aCRLFSplit)+1)*$iControlBuffer) + (UBound($aCRLFSplit)*$iControlHeight)
$hGui = GUICreate("something",$iGuiWidth,$iGuiHeight)

For $i = 0 To UBound($aCRLFSplit)-1
    $aCRLFSplit[$i] = StringSplit($aCRLFSplit[$i],"||||",3)
    GUICtrlCreateCombo("Line" & $i+1 & "'s split",$iControlBuffer,$iControlBuffer+($i*$iControlBuffer+$i*$iControlHeight),$iControlWidth,$iControlHeight)
    GUICtrlSetData(-1,_ArrayToString($aCRLFSplit[$i]))
Next

GUISetState(@SW_SHOW)
Do
    Local $msg = GUIGetMsg()
Until $msg = $GUI_EVENT_CLOSE
Exit
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Boom...shot that moving target...

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***

#include <array.au3>
#AutoIt3Wrapper_Add_Constants=n

local $str

$str &= 'Info1||||Info2||||Info3||||Info4' & @lf
$str &= 'Info5||||Info6||||Info7||||Info8' & @lf
$str &= 'Info9||||Info10||||Info11||||Info12'

local $array = stringsplit($str,@lf,2)

local $gui = guicreate('Huh ??!???')

local $lv = guictrlcreatelistview('Item1|Item2|Item3|Item4',10,10,300,300)

for $1 = 0 to ubound($array) - 1
    ConsoleWrite($array[$1] & @LF)
    GUICtrlCreateListViewItem(stringreplace($array[$1],'||||','|'),$lv)
next

guisetstate()

while 1
    switch guigetmsg()
        Case $GUI_EVENT_CLOSE
            Exit
    endswitch
wend

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

copied, and made dynamic :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <array.au3>

$string = "Info1||||Info2||||Info3||||Info4" & @CRLF & _
"Info5||||Info6||||Info7||||Info8" & @CRLF & _
"Info9||||Info10||||Info11||||Info12"
;~ ConsoleWrite($string & @CRLF)
$aCRLFSplit = StringSplit($string,@CRLF,3)
For $i = 0 To UBound($aCRLFSplit)-1
    $aCRLFSplit[$i] = StringSplit($aCRLFSplit[$i],"||||",3)
Next

$iSubItems = 0
For $i = 0 To UBound($aCRLFSplit)-1
    $aTemp = $aCRLFSplit[$i]
    If UBound($aTemp) > $iSubItems Then $iSubItems = UBound($aTemp)
Next

For $i = 1 To $iSubItems
    If $i = 1 Then
        $sHeader = "Item" & $i
    Else
        $sHeader &= "|Item" & $i
    EndIf
Next

$iGuiWidth = 75*$iSubItems
$iControlHeight = 20
$iControlBuffer = 10
$iControlWidth  = $iGuiWidth-$iControlBuffer*2
$iGuiHeight = (2*$iControlBuffer) + (UBound($aCRLFSplit)*$iControlHeight)+20
$hGui = GUICreate("something",$iGuiWidth,$iGuiHeight)
local $lv = guictrlcreatelistview($sHeader,$iControlBuffer,$iControlBuffer,$iGuiWidth-$iControlBuffer*2,$iGuiHeight-$iControlBuffer*2)
for $i = 0 to ubound($aCRLFSplit) - 1
    GUICtrlCreateListViewItem(_ArrayToString($aCRLFSplit[$i]),$lv)
next

GUISetState(@SW_SHOW)
Do
    Local $msg = GUIGetMsg()
Until $msg = $GUI_EVENT_CLOSE
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
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...