Sign in to follow this  
Followers 0
Stoex

StringSplit Question

21 posts in this topic

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!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Here are apples  :)

#include <Array.au3>

$str = "ww" &@crlf& "ww" &@crlf& "ww" &@crlf& "ww"

$astr = StringSplit($str, @crlf, 3) ; $STR_ENTIRESPLIT + $STR_NOCOUNT

_ArrayDisplay($astr)

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#11 ·  Posted

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

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I still think it's quite useful as a feature. It could go to 16 dimensions with a few tweaks. :D

Edited by czardas

Share this post


Link to post
Share on other sites

#13 ·  Posted

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")

Share this post


Link to post
Share on other sites

#14 ·  Posted

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

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Have you tried:

$rows = StringSplit($csv, @CRLF, 3)

;

Edit 

I guess I was reading ther wrong line. You code should not be returning the count in the first element.

Edited by czardas

Share this post


Link to post
Share on other sites

#16 ·  Posted

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.

Share this post


Link to post
Share on other sites

#17 ·  Posted

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

Share this post


Link to post
Share on other sites

#18 ·  Posted

BrewManNH

You are way more advanced than I am, so I'm a little confused now.

how is that wrong?

If i change it to ABC[2] [0] =$row [0] ????

Thanks for any enlightenment you can spare.

Share this post


Link to post
Share on other sites

#19 ·  Posted

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

Share this post


Link to post
Share on other sites

#20 ·  Posted

$ABC [0] [0]

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  
Followers 0