Page 1 of 1 1
Topic Options
#66293 - 2002-06-06 11:04 PM count number of times something is found
Jake Offline
Fresh Scripter

Registered: 2001-11-14
Posts: 26
Does anyone have a script to look for X number of times something is found??
for example:
code:
$variable = what to look for
$file = file to open
open $file
$line = readline (1)
$x = 1
$result = $x # of times found
open $file
while @error = 0
if instr($line,$variable)
$result = $x
endif
$line = readline(1)
$x = $x + 1
loop
close $file
? $result

I can't seem to the code right. Any help appreciated.

Top
#66294 - 2002-06-06 11:21 PM Re: count number of times something is found
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
not tested, but looks about right.

code:
$file="c:\temp.txt"
$string="whatever"
$length=len($string)

$count=0

$=open(1,$file,1)
$line=readline(1)
while @error=0
:lookagain
$pos=instrrev($line,$string)
if $pos
$count=$count+1
$string=left($string,$pos)
goto lookagain
endif
$line=readline(1)
loop
$=close(1)
? $count

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#66295 - 2002-06-07 12:32 AM Re: count number of times something is found
Jake Offline
Fresh Scripter

Registered: 2001-11-14
Posts: 26
Thanks for the fast reply. When I ran the script, I get a 0 for result. Any idea why?
code:
  
$file="%temp%\text.txt"
$string="*"
$length=len($string)
$count=0
$=open(1,$file,1)
$line=readline(1)
while @error=0
:lookagain
$pos=instrrev($line,$string)
if $pos
$count=$count+1
$string=left($string,$pos)
goto lookagain
endif
$line=readline(1)
loop
$=close(1)
? $count

example text.txt file:
ientv*no,now d* iernvi*
*lm*ohnv.*

[ 07 June 2002, 00:38: Message edited by: Jake ]

Top
#66296 - 2002-06-07 12:40 AM Re: count number of times something is found
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
oops....
I had this wrong... $line=left($line,$pos)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#66297 - 2002-06-07 12:50 AM Re: count number of times something is found
Jake Offline
Fresh Scripter

Registered: 2001-11-14
Posts: 26
OK, I replaced this line:
code:
  $string=left($string,$pos)

with this line:
code:
  $line=left($line,$pos)

and still getting a 0 for result.

entire code:
code:
 
$file="%temp%\text.txt"
$string="*"
$length=len($string)
$count=0
$=open(1,$file,1)
$line=readline(1)
while @error=0
:lookagain
$pos=instrrev($line,$string)
if $pos
$count=$count+1
;$string=left($string,$pos)
$line=left($line,$pos)
goto lookagain
endif
$line=readline(1)
loop
$=close(1)
? $count



[ 07 June 2002, 00:52: Message edited by: Jake ]

Top
#66298 - 2002-06-07 12:57 AM Re: count number of times something is found
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
change

code:
$line=left($line,$pos)  

to
code:
$line=left($line,$pos-1)  

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#66299 - 2002-06-07 01:01 AM Re: count number of times something is found
Jake Offline
Fresh Scripter

Registered: 2001-11-14
Posts: 26
I'm still getting a 0 for result. It's not incrementing when the $string if found for some reason.
Top
#66300 - 2002-06-07 01:01 AM Re: count number of times something is found
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Also change
code:
$=open(1,$file,1)  

to
code:
$=open(1,$file,2)  

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#66301 - 2002-06-07 01:11 AM Re: count number of times something is found
Jake Offline
Fresh Scripter

Registered: 2001-11-14
Posts: 26
Running this script in debug mode after the changes, I received the following error:
"Script error: Expected expression!
$pos=instrrev($line,$string)"

Top
#66302 - 2002-06-07 01:12 AM Re: count number of times something is found
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
The file was never open to READ. Once you change the OPEN command the script works.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#66303 - 2002-06-07 01:22 AM Re: count number of times something is found
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
see.. now you know why I only have 3 stars [Smile]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#66304 - 2002-06-07 01:27 AM Re: count number of times something is found
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You provided everything required. You stated it wasn't tested. All I did was a little debugging. [Wink]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#66305 - 2002-06-07 01:31 AM Re: count number of times something is found
Jake Offline
Fresh Scripter

Registered: 2001-11-14
Posts: 26
Well, I have none, that's why I'm asking for help [Wink] . But all kiddings aside, I've implemented the suggested changes and still receive the error mentioned in previous post.
Here's the changed code:
code:
  
$file="%temp%\text.txt"
$string="*"
$length=len($string)
$count=0
$=open(1,$file,2)
$line=readline(1)
while @error=0
:lookagain
$pos=instrrev($line,$string)
if $pos
$count=$count+1
;$string=left($string,$pos)
$line=left($line,$pos-1)
goto lookagain
endif
$line=readline(1)
loop
$=close(1)
? $count



[ 07 June 2002, 01:36: Message edited by: Jake ]

Top
#66306 - 2002-06-07 01:35 AM Re: count number of times something is found
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
This is the code that works for me.
code:
break on
$file="c:\data\scripts\junk.txt"
$string="*"
$length=len($string)
$count=0
$=open(1,$file,2)
$line=readline(1)
while @error=0
:lookagain
$pos=instrrev($line,$string)
if $pos
$count=$count+1
$line=left($line,$pos-1)
goto lookagain
endif
$line=readline(1)
loop
$=close(1)
? "Count of '" + $string + "' = " + $count




[ 07 June 2002, 01:36: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#66307 - 2002-06-07 01:40 AM Re: count number of times something is found
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
This code requires Kix 4.02 to support instrrev($line,$string). Are you using KiXtart 4.02?

[ 07 June 2002, 01:43: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#66308 - 2002-06-07 01:45 AM Re: count number of times something is found
Jake Offline
Fresh Scripter

Registered: 2001-11-14
Posts: 26
I download Kix4.02 and everything is working fine. It was a user error and not code.
Thanks both for your help.

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 646 anonymous users online.
Newest Members
min_seow, Audio, Hoschi, Comet, rrosell
17881 Registered Users

Generated in 0.05 seconds in which 0.021 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org