Jump to content
LisHawj

StringInStr - Am I using it correct?

Recommended Posts

LisHawj

I have a large file that contains thousands of license entries and I want to quickly verify if a new license request already exist for an object.  I start out with reading the large file that contains the licenses into an array, and then I use StringInStr to try find that value within that substring.  I wrote the following code, but I am unable to determine where I have gone wrong.  I only get the ELSE condition to return.  Please review and thank you for the help.  Thank you.

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $CN = GUICtrlRead($Input1)
            _FileReadToArray($Dir, $aArray)

            For $i = 1 To UBound($aArray) - 1
                If StringInStr($aArray[$i], $CN) Then
                    MsgBox(0,"License Finder result:", "License found for: " & $CN)
                    ExitLoop
                Else
                    MsgBox(0,"License Finder result:", "ERROR - NO License found for: " & $CN)
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd

 

Share this post


Link to post
Share on other sites
water

Could you try:

If StringInStr($aArray[$i], $CN) > 0 Then

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
mikell

Did you try to check the array after the _FileReadToArray with a _ArrayDisplay ?

Share this post


Link to post
Share on other sites
LisHawj

I still cannot get it to work.  I think the problem lies with how the strings are showing up in the array.  Below is a sample of the strings found in the large license file, and I think the StringInStr function is not able to find the target value "License83384" and etc in the substring when running through the array.  How do i break up the strings below to capture just the License numbers?  I can then write the information into a new array and think it may work. 

    <ValidationCode NetworkName="License83384" MachineId="28">4c2bf15fb1f4855b893f23be2e160c0c6ca9fae5d14c164aa76d2c7a28f77442</ValidationCode>
    <ValidationCode NetworkName="License83858" MachineId="44">4c2bf15fb18deb54e23c23be2e160c0c16d48b2919fe9d406efdb7e1babc2d3b</ValidationCode>
    <ValidationCode NetworkName="License83112" MachineId="69">4c2bf15fb181ee52853f23be2e160c0ca268e0b0b8b45c523a77ceeb58832d75</ValidationCode>

 

Share this post


Link to post
Share on other sites
LisHawj

@mikell

Yes, I had the following line in my previous revision to check the array output.  I think the array may need to be broken down more, but I don't know how to do that.  Below is a sample picture of what the arraydisplay returned.  The values in the yellow box are removed for privacy reasons.

   _FileReadToArray($Dir, $aArray)
   _ArrayDisplay($aArray, "1D array - count", Default, 8)

 

 

Capture.PNG

Edited by LisHawj
added picture.

Share this post


Link to post
Share on other sites
mikell

I just copied/pasted your text from post #4 into a "1.txt" file, and got no problem running this

$file = FileReadToArray("1.txt")
For $i = 0 to UBound($file)-1
    If StringInStr($file[$i], "License83384") Then Msgbox(0,"", "got it")
Next

 

Share this post


Link to post
Share on other sites
jdelaney

Use xmldom instead.   it will make life easy.   search the forums for examples.   then you can do one xpath query rather than looping through the file.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
mikell
6 minutes ago, jdelaney said:

Use xmldom instead. 

And why not a regex ? it would probably do the job nicely using 2 lines of code  :)

Share this post


Link to post
Share on other sites
jdelaney

I prefer a route that will always work, even when new attributes get added into, or removed out of, the xml.  personal preference.   of course you can spend the time to make the perfect regexp, but xmldom is simple.   all opinions, of course.  not to mention that manipulating is oodles easier with xmldom, but OP only wants to read.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
mikell

Well, matter of circumstances. I mentioned it because in the present case the OP's problem was quite simple
For the fun

$var = "License83384"

$res = StringRegExp(FileRead("1.txt"), $var & '.*?>([^<]+)', 1)
Msgbox(0,"", $var & (IsArray($res) ? " ? yeah, got it : " & $res[0] : " ? not found") )

 

Edited by mikell

Share this post


Link to post
Share on other sites
OldGuyWalking
On 2/9/2018 at 0:06 PM, jdelaney said:

I prefer a route that will always work, even when new attributes get added into, or removed out of, the xml.  personal preference.   of course you can spend the time to make the perfect regexp, but xmldom is simple.   all opinions, of course.  not to mention that manipulating is oodles easier with xmldom, but OP only wants to read.

I've seen XML DOM vs Regular Expressions comments related to making changes to XML documents in several posts.  What I haven't seen is a compelling argument that supports using XML DOM in all cases.  

The argument that XML DOM is "simple" is only true if the person using it understand how XML Documents are structured, understands the XML DOM properties and methods, understands the terminology that describes the various parts of a DOM document and how to navigate through the document to get to the information you want, and, most importantly,  understand how to troubleshoot the code when it fails to do what the user expected it to do.   That all requires time, research, trial and error, and is not simple.  But neither is RegEx.

If someone regularly works with XML documents adding nodes, deleteing nodes, changing attributes, changing structures, etc. then I'd absolutely agree that doing all of that with tools and coding that is XML DOM specific makes sense.   Then it makes sense to dive into the XML documentation and learn what it's many capabilities are. 

If someone just needs to occassionally pull a couple of pieces of data out of an XML file (or other trivial task)  then spending the time to learn XML DOM and XPath probably isn't a great investment in time. 

I'm not arguing for RegEX or against XML DOM.  I'm saying that there are alot of tools out there. Some are specialized and focus on one thing, like managing XML documents. Other tools are general, and in some cases, that makes them more flexible, like RegEx.

The best tool in one-off situations is the one the user is most experienced and familiar with that can perform the needed action.  Those situations are also when the K.I.S.S. rule must be applied.

I started learning XML DOM and XPath in November to process XML documents I receive through a sites API.  All I needed to do was extract data (in this case 8 fields from the TVSeries section, and 9 fields from multiple TVEpisode sections.  The XML documents are from TVDB, and they are pretty clean and easy to read.  

So,  I've been using regex for a few years.  I've extracted information from XML, NZB, HTML, text files, etc. using regex.   So, how hard could it be to do it with XML DOM?

After a week of frustration (and being appropriately humbled)  I spent 3 weeks reading material on Microsoft's XML DOM properties and methods, going through the XML DOM and XPath tutorials on the w3schools.com site, looking at every piece of code and discussions I could find in the AutoIT forums and any other forum, studying and trying to incorporate the XMLau3 wrapper into my code, downloaded and tried to make sense of Altova XML Spy.

After many headbanging against the top of my desk sessions, I was, finally, successful in getting the text I wanted.  Right now, I could probably tell you exactly how the code works but not necessarily why.

Being the masochistic sort, but realizing that XML DOM and XPath have their use, I'll continue learning both.

But for simple extractions of a few pieces of data from an XML or NZB file I'll use RegEx. 

It gets me the same outcome.  I get the data I need with minimal effort.

Unless someone has a better argument about the need to use XML DOM for every XML document I think I'll use the following criteria to make my decision on what tool to use.

1.  Is the data I want easy to extract from the XML document using regex? 

2.  Will it take the same or less effort to get the data using regex?

If the answer to both is yes then using regex makes sense.

==========================================================
Side note:

If anyone reading this is learning XPath, one of the things I struggled with was looking at a specific part of an XML document and identifying what it was and how to craft the XML path in order to navigate to that point and, in my particular case, how to extract the string I was after.   

I came across the following website : https://xmltoolbox.appspot.com/xpath_generator.html 

Copy and paste your XML document into the XML field on their page.  Note: Remove anything that's not meant to be seen by anyone else, i.e. proprietary information, confidential information, etc.   You can then  click on any part of the XML document code and the XPATH code to get there will be displayed in an XPath field that's directly below the XML field. As you you click on different locations, the XPath results will stack up in the XPath box. 

You can copy and paste these paths for use in your code.

 

 

 

Share this post


Link to post
Share on other sites
jchd

Storing license information in a SQLite database would be ideal: no issue with concurrent R/W, data integrity, instant lookup for whatever search criterion, easy addup of new attributes, etc.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×