Jump to content
Sign in to follow this  
kadanefewi

How to get line number where a string was found?

Recommended Posts

kadanefewi

Hey guys.

I need to obtain the line number where a given string was found, I couldn't find a function.

All I can think of is counting the number of CR & LF characters, would that work? How can I represent them to Autoit, if that's the best solution?

Thanks for the input.

Share this post


Link to post
Share on other sites
BrewManNH

You could use FileReadToArray and search the array to find where the string is located, the array row would be equivalent to the line number.


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
mikell

StringSplit on @crlf may be enough

Local $sString = "alpha" &@crlf& "beta" &@crlf& "gamma" &@crlf& "delta" &@crlf& "epsilon"
Msgbox(0,"", $sString)

Local $n = -1, $word = "gam"
$aString = StringSplit($sString, @crlf, 3)
For $i = 0 to UBound($aString)-1
   If StringInStr($aString[$i], $word) Then $n = $i
Next
Msgbox(0,"", '"' & $word & '"' & (($n = -1) ? " not found" : " found on line " & $n+1))

 

Share this post


Link to post
Share on other sites
BrewManNH

Unless it's a file he's reading from, then he'd still need a FileRead just to convert the read text to an array with StringSplit which FileReadToArray already does for you.


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
mikell

Right  :)

Share this post


Link to post
Share on other sites
Malkey

Here is another method - no array, only strings.

; Local $sString = FileRead("FullPath\FileName") ; If viewing file contents for script's accuracy, word wrap may cause visual errors.
Local $sString = "alpha" & @CRLF & "beta" & @CRLF & "gamma" & @CRLF & "delta" & @CRLF & "epsilon & gamble game"
MsgBox(0, "Original String", $sString, 3)

Local $n, $word = "gam", $iCharPos = -1, $Start = 1, $sRes = ""
While $iCharPos <> 0
    $iCharPos = StringInStr($sString, $word, 1, 1, $Start) ;  $STR_CASESENSE (1) = case sensitive, occurrence = 1
    StringRegExpReplace(StringMid($sString, 1, $iCharPos), "\R", "") ; By default \R matches @CRLF, @CR or @LF only.
    $n = @extended + 1 ; @extended contains the number of replacements performed.
    $Start = $iCharPos + 1
    If ($sRes = "") Or ($iCharPos <> 0) Then $sRes &= '"' & $word & '"' & (($iCharPos = 0) ? " not found" : " found on line " & $n) & @CRLF
WEnd

MsgBox(0, "Result", $sRes)

 

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

    • nitekram
      By nitekram
      I have passed my code to another co-worker and he said when he runs the script, he gets an ERROR on line 9544 - the problem, I have only 7100 lines in my script - I was wondering if there is a command/function, that I can put in my script to tell me what UDF file is creating the error?

      I am trying to drill it down, but was hoping that there was some indication within the error that would help -

      Line 9544 (File "path to exe"):

      Error: Variable used without being declared

      Edit: ps, I went through my code after adding Opt('MustDeclareVars', 1), and declared all my variables
    • dv8
      By dv8
      Hi guys,
      please bare with me for a moment here...

      My goal: To be able to take the error line number returned by the EXE file and locate the actual line in the source.
      I believe most of the AutoIt users are coming to this issue at some point.

      I realize the final source, before the compilation is done, is the combined source of all the include files and the main script etc. So I figured if I have a way to get this combined source, that would give me a solution to this problem. And I spent the last couple of days (more than 10 hours a day) searching in the forum and asking some questions in different threads. And with the help of few people, I found a way (using the Obfuscator) to get this source and the line numbers were actually MATCHING!

      However this solution introduced a WHOLE LOT of other problems for me and broke some of the functionality in my script since it has a lot of Call() and ObjEvent() functions. My code turned out to be un-obfuscatable. And for the record, I do not need any obfuscation done at all, nor I need the unused included functions stripped down (even though it made my EXE a 30% smaller). I needed the Obfuscator just to get this final piece of code to be able to achieve My goal.

      So all this got me thinking... Please correct me if I'm wrong, because I don not have any knowledge of the internals of a compiler.
      Since Aut2exe is the one that combines all the code together and then working it's magic to create the EXE, there should be a pint in this process where that code is available in its final form. Then why isn't there a command line option to dump the final combined source in a file just before it is compiled?
      Once again, I do not know the internals of the compiler but my logic dictates that this would be the most reasonable and straightforward solution.

      Please share your thoughts and also, if any of you knows a way to achieve my goal without using the Obfuscator, I would LOVE to here it!

      EDIT: And here is the solution: ( I've tried to explain it as simply as I can, so even the beginners can understand it.)



×