This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Searching from more than one File

Old posts that have not been replied to for several years.
Locked
h
him

Searching from more than one File

Post by him »

hey a while back User posted some code here, and what he posted was something alone the lines of what i needed. But my question is can it be possible to have it search from more than one text file?

his code was:

Code: Select all

bind pub - @find find:pub 
proc find:pub {n u h c a} { 
  set a *[string map {" " *} $a]* 
  putserv "PRIVMSG $n :Searching for $a" 
  set i 0 
  set f [open "search.txt"] 
  while {[gets $f b]>-1} { 
    if {[string match -nocase $a $b]} { 
      incr i 
      putserv "PRIVMSG $n :$b" 
    } 
  } 
  close $f 
  if {$i} { 
    putserv "PRIVMSG $n :Search complete found $i result[expr {$i==1?"":"s"}]" 
  } { 
    putserv "PRIVMSG $n :No matches." 
  } 
} 
what i'm woundering is how can i have it look around in 4 or 5 more files?
i tried adding some more calls but they all give errors:

Code: Select all

set m [open "search2.txt"] 
set p [open "search3.txt"] 
set x [open "search4.txt"] 
while {[gets $f,$m,$p,$x b]>-1}
close $m
close $p
close $x
but all i get is errors.. can anyone help me out?
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Its like, first you open one file, read it then close it.
Then open another one, read it then close it.

You cannot open 2 at the same time and read, them
I guess. You always need to close the last opened file
to be able to open/read the new one. Otherwise you
will get errors.

Give, this a try maybe it will do your job:
(Otherwise, open, read and close one by one!)

Code: Select all

catch {close $file}
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Try this

Post by user »

Code: Select all

# list of files to search:
set findfiles [list search.txt search2.txt search3.txt search4.txt]
set maxresults 10;# set to 0 for no limit
bind pub - @find find:pub
proc find:pub {n u h c a} {
	global findfiles maxresults
	set a *[string map {" " *} $a]*
	set out {}
	foreach file $::findfiles {
		if {[catch {open $file} f]} {
			putlog "find:pub - You're a moron: $f"
		} {
			while {[gets $f b]>-1} {
				if {[string match -nocase $a $b]} {
					lappend out $b
				}
			}
			close $f
		}
	}
	if {[set i [llength $out]]} {
		if {$maxresults&&$i>$maxresults} {
			putserv "PRIVMSG $n :Found $i match[expr {$i==1?"":"es"}] (here's the first $maxresults):"
			set out [lrange $out 0 [expr {$maxresults-1}]]
		} {
			putserv "PRIVMSG $n :Found $i match[expr {$i==1?"":"es"}]:"
		}
		foreach b $out {putserv "PRIVMSG $n :$b"}
	} {
		putserv "PRIVMSG $n :No matches."
	}
}
awyeah: there's nothing wrong with opening several files at once (as long as you don't leave them open and forget about them), but in this particular case, reading the files one by makes the script more flexible. :)
Have you ever read "The Manual"?
h
him

Post by him »

Thanx for your help guy's... :D
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Thats, why I say user is an elite scripter, here. I mean one of the best. I'm just a newbie. :mrgreen:
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Locked