Jump to content

Recommended Posts

Posted (edited)

Dim $oneDarray
$oneDarray=StringSplit($Rawfile, @CRLF, 1)
$columnsCounter = stringsplit($oneDarray[2],",")
     GUICtrlSetData ( $Output,"*Group columns Detected from csv:"& $columnsCounter[0] &@CRLF, @CR)
     ConsoleWrite("*Group columns Detected from csv:"& $columnsCounter[0] &@CRLF)
Dim $twoDarray[$oneDarray[0] + 1][$columnsCounter[0] + 1]
For $x = 1 to ($oneDarray[0])
$oneRow = stringsplit($oneDarray[$x],",")
For $y = 1 to ($oneRow[0])
   $twoDarray[$x][$y] = $oneRow[$y]
Next
Next


Log Output
Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
$twoDarray[$x][$y] = $oneRow[$y]
^ ERROR

So I have a csv file that I'm breaking up to do string parsing for information from and I've run into a problem I'm not sure how to solve.

In the code shown above I am creating the master 2d array that holds all the values from the csv. The problem the code runs into starts on this line: $oneRow = stringsplit($oneDarray[$x],",")

It worked great until I ran into a csv file that had commas in a txt field that were not related to the csv format.. example:

"this is some text, and some more text", filedir, ipaddress, hostname,mmmbeer

as CSV format this is 5 fields, however the stringsplit counts the comma in the text and identifies this as 6 fields.

Any ideas how I can somehow not include the comma in quotes in stringsplit?

Thanks,

Bob

Edited by bobsyuruncle
Posted (edited)

yes. isolate the commas in quotes (with stringinstr and/or stringsplit) and replace them with something exotic like "§#". after splitting replace the commas back.

E.

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

  • Solution
Posted (edited)

If the string with the comma inside is effectively quoted, this should work

#include <Array.au3>
$str = '"this is some text, and some more text", filedir, ipaddress, hostname,mmmbeer'
$res = StringRegExp($str, '[^,"]+|("[^"]*"\h*)', 3)
_ArrayDisplay($res)
Edited by mikell
Posted (edited)

ha i knew there comes a regex, as soon as i am ready with my version ;)

.

$y=stringsplit($oneDarray[$x],'"')
If $y[0]>1 Then
    $oneDarray[$x]=$y[1]
    For $i=2 To $y[0] Step 2
        $oneDarray[$x]&='"'&StringReplace($y[$i],",","§#")&'"'&$y[$i+1]
    Next
EndIf

.

E.

Edit: take mikell's, can't beat it :)

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Posted

Thanks for the tips gents.

Both solutions should work fine for anybody, but I'm going to mark mikell for solved. I've never worked with regex but I can see now I should spend some time with it for this kind of project.

Posted

regex sure is way cooler, but if you don't understand it, it's a risk. you will never be able to tackle errors or to change the syntax. just to consider.

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

  • 1 year later...
Posted (edited)

Your way doesn't support empty fields (e.g. "like,,,,this").

Here's a regexp code that does (change '[^,\"]+| to '[^,\s"]+| if you want spaces as delimiters too).

On 15.8.2013 at 6:13 AM, mikell said:

If the string with the comma inside is effectively quoted, this should work

#include <Array.au3>
$str = '"this is some text, and some more text", filedir, ipaddress, hostname,mmmbeer'
$res = StringRegExp($str, '[^,"]+|("[^"]*"\h*)', 3)
_ArrayDisplay($res)

 

Edited by LWC
Note about spaces
Posted

2 years too late. Please don't bring up old threads in the future. Thank you.

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

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
×
×
  • Create New...