#66293 - 2002-06-06 11:04 PM
count number of times something is found
|
Jake
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
   
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
|
Top
|
|
|
|
#66295 - 2002-06-07 12:32 AM
Re: count number of times something is found
|
Jake
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
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
oops.... I had this wrong... $line=left($line,$pos)
|
Top
|
|
|
|
#66297 - 2002-06-07 12:50 AM
Re: count number of times something is found
|
Jake
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
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)
|
Top
|
|
|
|
#66299 - 2002-06-07 01:01 AM
Re: count number of times something is found
|
Jake
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
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)
|
Top
|
|
|
|
#66301 - 2002-06-07 01:11 AM
Re: count number of times something is found
|
Jake
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
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.
|
Top
|
|
|
|
#66303 - 2002-06-07 01:22 AM
Re: count number of times something is found
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
see.. now you know why I only have 3 stars
|
Top
|
|
|
|
#66304 - 2002-06-07 01:27 AM
Re: count number of times something is found
|
Howard Bullock
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.
|
Top
|
|
|
|
#66305 - 2002-06-07 01:31 AM
Re: count number of times something is found
|
Jake
Fresh Scripter
Registered: 2001-11-14
Posts: 26
|
Well, I have none, that's why I'm asking for help . 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
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 ]
|
Top
|
|
|
|
#66307 - 2002-06-07 01:40 AM
Re: count number of times something is found
|
Howard Bullock
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 ]
|
Top
|
|
|
|
#66308 - 2002-06-07 01:45 AM
Re: count number of times something is found
|
Jake
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
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 646 anonymous users online.
|
|
|