Jump to content
Sign in to follow this  
Hypertrophy

reading last occurence of certain strings

Recommended Posts

Hypertrophy

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.

Share this post


Link to post
Share on other sites
Jos

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Hypertrophy

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.

Share this post


Link to post
Share on other sites
AdmiralAlkex

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? >_<

Share this post


Link to post
Share on other sites
Hypertrophy

Is that possible? I know there is a occurence parameter but would I manually have to specify the occurence? Or could I use -1 to find the last occurence? How do I do that?

Edited by Hypertrophy

Share this post


Link to post
Share on other sites
Hypertrophy

So why are you not telling StringInStr() to find the last occurance? >_<

HOW DO I DO THIS

Share this post


Link to post
Share on other sites
BrettF

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)

>_>

Share this post


Link to post
Share on other sites
Hypertrophy

This'll only work for the last occurrence on a single line. My text file may be 1000 lines of data, like in my OP.

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.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
BrettF

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.

Share this post


Link to post
Share on other sites
trancexx

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

Share this post


Link to post
Share on other sites
Hypertrophy

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?

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.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
enaiman

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

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  

×