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.

Write and Read

Help for those learning Tcl or writing their own scripts.
Post Reply
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Write and Read

Post by Elfriede »

Good evening :)

I've the following Code done by myself, which i'm proud of ^^

Code: Select all

bind pubm - "*" pub_write

proc pub_write {nick uhost hand chan text} {
set text [stripcodes bcruag $text]
if {$nick == "Blub" & $chan == "#testchan"} {
set text [split $text "\n"]
foreach line $text {
set fileId [open test.txt "a"]
puts $fileId $text
close $fileId
  }
 }
}
Each line of Nick=Blub is written into the test.txt

This looks like:
{[Spain2007] Family.and.Friends.2007}
1st Question: Is it "normal" that those Brackets {} have been added automaticly ?

2nd: In near future the list will be getting longer and longer

Is it possible to search in this .txt File. although there are those Brackets and how could a searche engine look like eg:

!search spain or !search family so that every line will be printed that contains a Word of the .txt File in a Channel

Please help :)

Kind Regards
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

1: Those are added since you are converting the text-line into a list (splitting by newline). Since the text-parameter will always contain a single line, I'm not sure this is what you intended. {} would be added to encapsulate the list item (the text string) whenever it contains any "special" characters or whitespaces that would otherwize interfere with list structures.

2: Searching would be quite possible using various commands such as "string match". Concern must however be taken to whatever format is used when lines were added. I would however suggest adding lines of text, rather than lines of lists.

Also, your foreach-list is flawed, as you use the variable "text" within the loop rather than the iterated variable "line". Furthermore, you'd be better off opening and closing the file outside the loop.

Even further, & is "bit-wize and-operator", and is not recommended for logical comparisons, use && instead. Also considder using "string equal" to allow non case-sensitive comparisons.

Finally, as stated in answer to q; "text" will always contain one single line, so using that foreach-loop along with splitting is completely pointless.

I'd suggest something like this instead:

Code: Select all

bind pubm - "*" pub_write

proc pub_write {nick uhost hand chan text} {
 set text [stripcodes bcruag $text]
 if {[string equal -nocase $nick "Blub"] && [string equal -nocase $chan "#testchan"]} {
  set fileId [open test.txt "a"]
  puts $fileId $text
  close $fileId
 }
}
Searching in this case would be a matter of opening the file, read one line at a time using gets, and check it against your credentials..
Another option might be to open it and read all data using the "read" command, then split the data on newlines, and use lsearch..
NML_375
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Post by Elfriede »

Thank you very much ! Especially for detailed answer!
I've now changed the Code :)

Can someone please give me an example, how the search can work ? :)
[Spain2007] Family.and.Friends.2007
[GB2004] Tim.Tom.Sabine.2004
[Italy1998] Grandmother.Uncle.and.ugly.wife1998 ^^
eg: posted above:

!search Family or !search Grandmother.Uncle - including two or more Words in the search.

I'm sorry for asking, but i have absolutely no idea :(
[/quote]
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

To match these strings, you can use something like this:

Code: Select all

if {[regexp {^[A-Z]$} [string index $text 0]] && [regexp -nocase {^[a-z.]{1,}$} [string range $text 1 end-4]] && [regexp {^[0-9]{4}$} [string range $text end-3 end]]} {
Might not be the best of solutions, (not a regexp pro).. but should do the job for you.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Post by Elfriede »

Thank you very much, but i am sorry to say that i've no idea for the rest of the Code :(

Can someone please help me ? :)
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Err sorry, my last post was to tell you, how to match those three lines, for that I gave you a regexp code.

Now what is it, that you want your script to do? I don't clearly understand your purpose.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Post by Elfriede »

Those three lines are examples of the content. I would like to have .. i don't how its correctly called.. a search engine.

A search Engine, that reads the complete text file and print those lines, that includes the Matchword(s).

edit: Maybe someone can help me another way. I have exactly found, what i was looking for, BUT the Code is written in Mirc :( Maybe somone please can change the following lines to tcl

Code: Select all

;# Command: !search <Search String>
on *:TEXT:!search*:#:{
  if ($2-) Mod.Search.aTXT Test.txt $chan $ifmatch
  else .notice $nick Enter Search String!
}

;# Befehl: /Mod.Search.aTXT <TXT File> <#Channel> <Search String>
alias Mod.Search.aTXT {
  var %a = 1, %b = $lines($1), %text = $strip($3-)
  while (%a <= %b) {
    if (%text $iif($chr(42) isin %text, iswm, isin) $read($1, %a)) msg $2 $v2
    inc %a
  }
} 
Post Reply