sparrow925

StringSplit appending row numbers

12 posts in this topic

Seems simple enough -- I'm taking user input in this form

Quote

100-10-7
100-11-8
100-12-9
100-14-1

And searching an array for each item. So I used StringSplit to break up their input by '@CR and fed it into a loop to apply _ArraySearch. Much to my disappointment, it only finds the first value and reports everything after that as "not found." This confused me, until I went to look at the output of StringSplit with _ArrayDisplay, and used the 'copy data' button and pasted into TextEdit, giving: 

Quote

4
100-10-7
[2]|100-11-8
[3]|100-12-9
[4]|100-14-1

It seems to be appending row numbers to all rows beyond the first two. This is the only explanation I can find for why my search is failing on all items after the first.  (And For my tests I'm taking numbers directly from the list being searched... I know they are in there.)

Any insights? Code follows.

 

Local $CASlist = GUICtrlRead($Edit1) ; get search data from input
            Local $aCAS = StringSplit($CASlist, @CR) ;[0] is count of rows, [1] is first item
            Local $aresultlist[1]

            ;Loop this for each $CAS in $CASlist search and add result to results array
            $i = 1
            $100=100
            ProgressOn("Searching for CASRNs...", "(Press ESC to quit)")

            Do
               ProgressSet((($i*$100)/(UBound($aCAS)-1)), "(Press ESC to kill script)", "Currently Processing: "&$i&" out of "&(UBound($aCAS)-1))

               Local $searchresult = _ArraySearch($aGlobalInvs, $aCAS[$i], 0, 0, 0, 1)
               If @error=6 Then $searchresult = "Error: CASRN Not Found"
               If @error<>6 And @error<>0 Then $searchresult = "Error (_ArraySearch error NOT 6)"

               _ArrayAdd($aresultlist, $searchresult) ;$aresultlist has empty [0], [1] is first search result
               If @error Then $searchresult = "Error: "&@error

               $i+=1
            Until $i = UBound($aCAS)

            ProgressOff()

            ;MsgBox(0,0,$searchresult&" : "&@error)
            _ArrayDisplay($aresultlist)
            If @error Then MsgBox(0,0,"Error: "&@error)

 

Share this post


Link to post
Share on other sites



Looks like you're using "Copy Data & Hdr/Row" instead of "Copy Data Only" in the ArrayDisplay popup?

Your code syntax is fine, from the looks of it, so hard to tell what's going on. Probably some misunderstanding about how the data in the edit and the data in that array relate. Please write a standalone reproducer where you put some data in that array and in that string that you are splitting, so we can look what's happening.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hey, wait... Maybe your line endings are not @CR but @CRLF? Try splitting on that. /edit: remember to use $STR_ENTIRESPLIT option with StringSplit.

 
Edited by SadBunny
1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

sparrow925,

I believe SadBunny is right on both accounts.  Try populating your edit control like this...

local $gui010 = guicreate('EOL Test')
local $edt010 = guictrlcreateedit('',20,20,100,300)
local $btn010 = guictrlcreatebutton('Dump Edit Control To Console',20,350,340,25)
guisetstate()

local $aTest = ['100-10-7','100-11-8','100-12-9','100-14-1']
for $1 = 0 to ubound($aTest) - 1
    guictrlsetdata($edt010,$aTest[$1] & @CRLF,1)
next

while 1
    switch guigetmsg()
        case -3
            Exit
        Case $btn010
            ConsoleWrite(guictrlread($edt010) & @CRLF)
    endswitch
wend

and split on @CRLF using $STR_ENTIRESPLIT as advised.

You will get better assistance if you post a runnable reproducer.

kylomas

1 person likes this

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for the response! I tried separating by @CRLF at first, but that gave an array with empty entries in-between each number, so I downsized to @CR and it seemed to split nicely.

 

Taking a guess at what you mean by standalone reproducer, as I am an unprofessional hack making her way through this world on guts and glory -- here's the whole thing.

//Edit to add:

(When you look at $aCAS in ArrayDisplay it /looks/ fine. It's the copy-and-paste that seems to reveal what I think is the problem. //

 

(an abridged version of the GICmaster csv is attached, the $filepath variable will need to reflect where that is put)

 

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

; ***************************************************************
; Set Hot Key to Kill Script on Esc
; ***************************************************************
HotKeySet("{ESC}", "Terminate")
Func Terminate()
      Exit
   EndFunc

; ***************************************************************
; Read csv with inventory information in it
; ***************************************************************
$sfilepath = "C:\Users\user\Documents\GICmaster.csv"
$aGlobalInvs = FileReadToArray($sfilepath) ;[0] is column names, [1] is first entry

; ***************************************************************
; GUI
; ***************************************************************

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Global Inv+ Lookup", 249, 259, -1, -1)
Global $Edit1 = GUICtrlCreateEdit("", 8, 32, 137, 217)
Global $Label1 = GUICtrlCreateLabel("Paste CASRN from RDM:", 8, 8, 125, 17)
Global $Button1 = GUICtrlCreateButton(">>", 160, 112, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button1
            Local $CASlist = GUICtrlRead($Edit1) ; get search data from input
            Local $aCAS = StringSplit($CASlist, @CR) ;[0] is count of rows, [1] is first item
            Local $aresultlist[1]

            ;Loop this for each $CAS in $CASlist search and add result to results array
            $i = 1
            $100=100
            ProgressOn("Searching for CASRNs...", "(Press ESC to quit)")

            Do
               ProgressSet((($i*$100)/(UBound($aCAS)-1)), "(Press ESC to kill script)", "Currently Processing: "&$i&" out of "&(UBound($aCAS)-1))

               Local $searchresult = _ArraySearch($aGlobalInvs, $aCAS[$i], 0, 0, 0, 1)
               If @error=6 Then $searchresult = "Error: CASRN Not Found"
               If @error<>6 And @error<>0 Then $searchresult = "Error (_ArraySearch error NOT 6)"

               _ArrayAdd($aresultlist, $searchresult) ;$aresultlist has empty [0], [1] is first search result
               If @error Then $searchresult = "Error: "&@error

               $i+=1
            Until $i = UBound($aCAS)

            ProgressOff()

            ;MsgBox(0,0,$searchresult&" : "&@error)
            _ArrayDisplay($aresultlist)
            _ArrayDisplay($aCAS)
            If @error Then MsgBox(0,0,"Error: "&@error)

    EndSwitch
WEnd

 

The user wants to copy and paste a column of numbers from excel to input into the GUI, so that's what I've been doing as I got this up and running.

Apologies for not using functions here, this is the very first pass, getting things to work (hypothetically anyways)

GICmaster.csv

Edited by sparrow925

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You are getting the blank lines because you are not using the parameter $STR_ENTIRESPLIT

edit: apologies for the curt reply but the dog is going ballistic at the back door.  It appears that I have company.

Edited by kylomas
note

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

Share this post


Link to post
Share on other sites

Yeah, copypasting from Excel would normally give you @CRLF line endings:

hnbW8Hr.png

 


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

So... If your split is done on CR, every line after the second will begin with a LF (line feed). Split as follows instead:

#include <Array.au3>

$text = "line 1" & @CRLF & "line 2" & @CRLF & "line 3"
_ArrayDisplay(StringSplit($text, @CRLF, $STR_ENTIRESPLIT))

 


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Ack, looks like I posted on top of @SadBunny's edit about $STR_ENTIRESPLIT and @kylomas's complete solution, then sped off to an errand. You guys have completely sorted out the issue, thank you both very much.

So, a standalone reproducer is a code chunk that doesn't need anything else to work, yeah? I'll be sure to make one next time I post, thanks for the heads up.

(Weirdly though, SadBunny, I was using the 'copy/paste data only' button when I was getting partial row headers. Not a problem, now that the root issue was sorted, but a silly weird thing~)

/edit for spelling

Edited by sparrow925

Share this post


Link to post
Share on other sites
Just now, sparrow925 said:

So, a standalone reproducer is a code chunk that doesn't need anything else to work, yeah? I'll be sure to make one next time I post, thanks for the heads up.

Yes, as short as possible and with as few "contingencies" as possible. Hardcoded data, no external files, no unnecessary commands etc.. This has multiple advantages:

  • As you approach the problem from a fresh perspective, you often come across something that you forgot/overlooked the first time around.
  • It eliminates (or indicates, if they go away :)) possible problems caused by other parts of the code that you may overlook in debugging.
  • It becomes much easier for others to try your code out and debug it for you.

You will find that 9 out of 10 times you have that facepalm moment halfway through writing the reproducer :) If nothing else, it teaches you to always compartmentalize your code so that it's easier to reuse and troubleshoot.

1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Screenshot'd that reply and made my work computer background. B) Thank you guys again for helping me out.

Share this post


Link to post
Share on other sites

@sparrow925 No problem, glad I could help, and I'm sure that goes for @kylomas as well :cheer:


Roses are FF0000, violets are 0000FF... All my base are belong to you.

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