Jump to content
Sign in to follow this  
bobsyuruncle

stringsplit using comma but with additional options

Recommended Posts

bobsyuruncle

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

Share this post


Link to post
Share on other sites
Edano

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]

Share this post


Link to post
Share on other sites
mikell

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
  • Like 1

Share this post


Link to post
Share on other sites
Edano

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]

Share this post


Link to post
Share on other sites
bobsyuruncle

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.

Share this post


Link to post
Share on other sites
Edano

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]

Share this post


Link to post
Share on other sites
LWC

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

Share this post


Link to post
Share on other sites
guinness

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

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  

  • Similar Content

    • nooneclose
      By nooneclose
      I need help turning this string "20180913221626" into a formatted time string.
      I need to go from this: 20180913221626
      to this: 09/13/2018 10:16 PM
      I do not always know what the date will be so I can not just use a variable I need to actually convert/format. 
      I did see an older post in the forms that was basically the same question only the other guy did not post the working code and I can not figure out how to use _AD_GetObjectProperties properly to get what I want. 
      As always any help would be appreciated. 
      Here is the code I use to find the date, but it always gives me the unformatted version. 
      ;retrieve the items object $oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default) $oItem.GetInspector $eSentOn = $oItem.SentOn ; When was the email sent? MsgBox("", "Sent On of the email", $eSentOn) ;******************************************************************************* ; Formats the date and time from the email ;******************************************************************************* ;Local $fDatenTime = _DateTimeFormat($eSentOn, 1) ;MsgBox("", "Formatted email time", $fDatenTime) $aProperties = _AD_GetObjectProperties($eSentOn) _ArrayDisplay($aProperties, "Did the conversion work?")  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Miliardsto
      By Miliardsto
      I got that func
      Func makeHelpImgGUI($title,$width,$height,$img) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, "HERE") EndFunc and I call this func like that
      makeHelpImgGUI("Image",1190, 800,$SETTINGS_JPG)  
      so what is the problem in the parameter where is - "HERE" I need value of img but passed as string
      so $img = $SETTINGS_JPG and how make it "SETTINGS_JPG"
       
      I tried something like that but not work
      Func makeHelpImgGUI($title,$width,$height,$img) $name_str = String($img) $name_str = StringTrimLeft ($name_str, 1 ) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, $name_str) EndFunc  
    • liagason
      By liagason
      Hello everyone,
      How can I display in ascending  sequence some numbers stored in a string variable?
      $str = "18,03,48,23" MsgBox(0,"test",$str) I would like it to display "03,18,23,48"
    • Rskm
      By Rskm
      Hi, I have the following line in a text file 'input.txt'. I know the line number - say '6'. I wish to replace the text 'WWW' in the below line with a random number (I can generate that with random()).
      WERIS  WWWJP   3.83  8.330  1.000                1097.RAXX 
      The WWW is a 3 digit integer (could be any number between 0 to 999), I can use stringtrimleft and get the numerical value of WWW in this file
      so, basically, I know the string to replace (ie; WWW stored in a variable), I know the line number to work on and the file location/name and the replacement variable (through random()). My requirement is to fill that 3 spaces with my random number (which Is a integer between 1 and 999)
      please put ur suggestions
       
×