Jump to content

StringSplit Question


Recommended Posts

I'm using the below code in an array and everything works as it should, including the element count(by default) in the 0 row.
I want to eliminate that count and have tried using:  $STR_NOCOUNT   but the count is still returned in row 0.
The documentation says I should be able to use $STR_NOCOUNT.  The examples don't fit what I'm doing.
 
$csv = FileRead ("C:ABC ExpressXYZ.csv")
$csv = StringStripWS($csv, 7)   
$rows = StringSplit($csv, @CRLF )
 
 
Seems straight forward but I'm hitting a brick wall.
What should I be doing with $STR_NOCOUNT to make this work?  Thanks!
Link to comment
Share on other sites

  • Moderators

Not exactly possible for us to compare "apples to apples" unless you plan on posting your csv file :)

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

As a beginner, I would have to say if you are not comparing apples to apples, the end results could be different.

But thanks.

You can compare apples to daleks if you like  the array count is simply not returned in a resulting array if you use the flag.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

If you have the latest beta version, you can use _FileReadToArray, doing something like this. It will read in a character delimited file, for example a CSV file, and split it to a 2D array for you.

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

Example()

Func Example()
    ; Define a variable to pass to _FileReadToArray.
    Local $aArray = _FileReadToArray("C:\ABC Express\XYZ.csv", $FRTA_NOCOUNT, ",") ; comma delimited file

    ; Read the current script file into an array using the variable defined previously.
    ; $iFlag is specified as 0 in which the array count will not be defined. Use UBound() to find the size of the array.
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
    EndIf

    ; Display the array in _ArrayDisplay.
    _ArrayDisplay($aArray)
EndFunc   ;==>Example

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Note Stoex that Array[0] will be filled no matter what flag you use. If you use $STR_NOCOUNT (or 2), it (Array[0]) wont be filled with the count of returned elements, but with the first element.

Local $sString = "this is a string"
Local $aString1 = StringSplit($sString, " ")
Local $aString2 = StringSplit($sString, " ", 2)
MsgBox(0, "", "Array[0] - " & $aString1[0] & @CRLF & "Array[0] - " & $aString2[0])
Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

If you have the latest beta version, you can use _FileReadToArray, doing something like this. It will read in a character delimited file, for example a CSV file, and split it to a 2D array for you.

 

Interesting! I imagine this doesn't cater for standard csv variants: commas,  line breaks and double quotes enclosed within fields.

Edited by czardas
Link to comment
Share on other sites

It's a basic CSV split, for those that just need a file with the standard delimiters used. It doesn't pay attention to quotes around items that might have the delimiter inside, e.g. "test, test1, test2" will be split like

  • "test
  • test1
  • test2"

You'd need a purpose built function for that, such as the one you posted.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

All interesting comments. Here is the code I referred to. Site won't let me upload CSV file, but its simple as the comment in the code mentions.

No matter what I tweak, I keep getting the element count in row 0.  So being new at this, I must be overlooking something.

Other than the element count in row 0, everything else is fine.

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

Local $iCount = 0, $sTemp
Local $ABC[200][5]                                       ;state array
$csv = FileRead ("C:\ABC Express\XYZ.csv")               ;5 data fields separated by comma
$csv = StringStripWS($csv, 7) 
$rows = StringSplit($csv, @CRLF )
Dim $ABC[$rows[0] + 1][5 + 1]
$ABC[0][0] = $rows[0]  
For $i = 1 to $rows[0]  
    $temp = StringSplit($rows[$i], ",",2 )
    For $j = 0 to UBound($temp) - 1
        $ABC[$i][$j] = $temp[$j] ;
        if $j = 4 then
          $ABC [$i] [$j] = number($ABC [$i] [$j])
          EndIf
         Next
      Next

    _ArrayDisplay($ABC, " Result")
Link to comment
Share on other sites

You're using stringsplit without the $STR_NOCOUNT parameter.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

in the code:

$rows = StringSplit($csv, @CRLF ) ;not using $STR_NOCOUNT parameter.

But 4 lines later..

$temp = StringSplit($rows[$i], ",",2 ) ; <<<< 2 is used for the $STR_NOCOUNT parameter as per format for StringSplit..

And I just tried $rows = StringSplit($csv, @CRLF,3 ) ; as suggested and it then only returns row 0 and nothing else.

Perplexing it works fine with the exception of returning the element count.

Link to comment
Share on other sites

MIssed that, sorry. I see the problem though.

$ABC[0][0] = $rows[0]  
For $i = 1 to $rows[0]

You're putting the count into $ABC yourself in that code, so no wonder it has the count there.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You're complaint was that you were getting the count in the array even when you are using StringSplit with the no count value.

Where are you getting the split count showing up? In $ABC[1][0], $ABC[2][0] etc. or in $ABC[0][0]?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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