Jump to content

reading last occurence of certain strings


Recommended Posts

how can i read a text file and search for a certain string and find the last occurrence of it? for example if my text file contains 1000 lines of data and the word "Table Settings of Game" appears 300 times in the file how can i find the last occurence of that string. Thanks for your help in advance.

Link to comment
Share on other sites

  • Developers

how can i read a text file and search for a certain string and find the last occurrence of it? for example if my text file contains 1000 lines of data and the word "Table Settings of Game" appears 300 times in the file how can i find the last occurence of that string. Thanks for your help in advance.

You are long enough here to know you have to show some effort yourself first.

So... what have you tried ?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Jos,

$file = "test.txt"  ; file i want to read from
If StringInStr($file, "Table Settings of Game") Then
    MsgBox(0,"","Found String, reading downward from this string...")
EndIf

But of course the above just looks to see if that string is anywhere in the file, I want it to find the last occurence and then read downward from that point.

Link to comment
Share on other sites

Jos,

$file = "test.txt"  ; file i want to read from
If StringInStr($file, "Table Settings of Game") Then
    MsgBox(0,"","Found String, reading downward from this string...")
EndIf

But of course the above just looks to see if that string is anywhere in the file, I want it to find the last occurence and then read downward from that point.

So why are you not telling StringInStr() to find the last occurance? >_<
Link to comment
Share on other sites

You cannot read the helpfile? COME ON!

$string = "I want to game.  I need to game.  Game!"
$sSearch = "game"

$pos = StringInStr ($string, $sSearch, 2, -1)

MsgBox (0, "", "String found at pos: " & $pos)

>_>

Link to comment
Share on other sites

Something like (pseudocode):

$lines = _FileCountLines()
for $i = $lines to 1 step -1
  $line = FileReadline(filehandle,$i)
  if stringinstr($line,"your search string") = 0 then
   msgbox(0,"","blablabla")
   exitloop
  endif
next
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Oh will it?

$string = "I want to game.  I need to game.  Game!" & @CRLF
$string &= "I want to game.  I need to game.  Game!" & @CRLF
$string &= "I want to game.  I need to game.  Game!"

$sSearch = "game"

$pos = StringInStr ($string, $sSearch, 2, -1)

MsgBox (0, "", "String found at pos: " & $pos)

Maybe if you understood that @CRLF was a character (OMG NO WAYZ!), you would realize that you're being stupid. But then again, with someone with <200 posts, I would expect them to at least try the fucking examples. Far out.

Link to comment
Share on other sites

Oh will it?

$string = "I want to game.  I need to game.  Game!" & @CRLF
$string &= "I want to game.  I need to game.  Game!" & @CRLF
$string &= "I want to game.  I need to game.  Game!"

$sSearch = "game"

$pos = StringInStr ($string, $sSearch, 2, -1)

MsgBox (0, "", "String found at pos: " & $pos)

Maybe if you understood that @CRLF was a character (OMG NO WAYZ!), you would realize that you're being stupid. But then again, with someone with <200 posts, I would expect them to at least try the fucking examples. Far out.

You are using wrong inequality sign.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Gosh, I wish I could curse you out too, but unfortunately I can't at the moment because I still need some help.

Anyway, I didn't understand that -1 would still work on completely new lines. I am new in this file reading parsing etc territory. So from your code it would return the position of the last occurrence of "game"? Am I correct?

How would I take that position and read downward until I reach the end of the file?

Link to comment
Share on other sites

What BrettF suggests is to read the whole file into one variable and then serach for the last occurrance of the search string.

So use FileRead to read the whole file into a variable and then use BrettF's code.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

It is obvious the OP has some issues with reading the help (I guess either he's not reading it or he doesn't read it attentively).

The living proof of my statement is in this quote from StringInStr

occurrence [optional] Which occurrence of the substring to find in the string. Use a negative occurrence to search from the right side. The default value is 1 (finds first occurrence).

I have tried to help him several days ago but in the end it proved to be a waste of my time so I've stayed out of his new threads.

BrettF - I understand your frustration mate, it's a normal reaction.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

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