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.

Open File, Search for Text, Output Text

Help for those learning Tcl or writing their own scripts.
Post Reply
I
InTeNsO
Voice
Posts: 5
Joined: Sat May 09, 2009 4:14 pm

Open File, Search for Text, Output Text

Post by InTeNsO »

I want to search for a line in a file and then output the complete line or parts of it.

The file looks like this. http://hearthstonejson.com/json/AllSets.enUS.json

Code: Select all

,{"id":"EX1_116","name":"Leeroy Jenkins","type":"Minion","faction":"Alliance","rarity":"Legendary","cost":4,"attack":6,"health":2,"text":"<b>Charge</b>. <b>Battlecry:</b> Summon two 1/1 Whelps for your opponent.","flavor":"At least he has Angry Chicken.","artist":"Gabe from Penny Arcade","collectible":true,"elite":true,"mechanics":["Battlecry","Charge"]},{"id":"EX1_029","name":"Leper Gnome","type":"Minion","faction":"Neutral","rarity":"Common","cost":1,"attack":2,"health":1,"text":"<b>Deathrattle:</b> Deal 2 damage to the enemy hero.","flavor":"He really just wants to be your friend, but the constant rejection is starting to really get to him.","artist":"Glenn Rane","collectible":true,"mechanics":["Deathrattle"]},

Code: Select all

putlog "\[\0032hscard.tcl\003\] loaded"

bind pub - !hs hscard
# !hs leeroy
	
proc hscard {nick uhost hand chan hscard} {
  if {$hscard == ""} { putserv "NOTICE $nick :!hs card" ;return 0 }
 
   set fd [open "scripts/cards.json" r] 
   set cont [read $fd] 
   close $fd 

   foreach line [split $cont "\n"] { 
      if {[string match -nocase "*$hscard*" $line]} { 
	  #regexp {.*?\"name\":\"$hscard\",(.*?)} $line - hs(name)
	  #putlog "$hs(name)"
         putserv "PRIVMSG $chan :'$hscard' is in the file!" 

         set found 1  ;  break 
      } 
   } 
   if {![info exists found]} { 
      putserv "PRIVMSG $chan :Text is not found.." 
   } 
} 
The Output should be somethins like this.
<bot> [Leeroy Jenkins]: 6/2: Cost: 4 - Usable by all classes - Charge. Battlecry: Summon two 1/1 Whelps for your opponent.[/code]

I don't know how to regexp/lsearch or whatever to read that one complete line that contains what i need.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Hi,

If the file you want to read is in JSON format why don't you use json::json2dict to transform it in a dictionary and then use dict's functions to fetch what info you want?
Once the game is over, the king and the pawn go back in the same box.
I
InTeNsO
Voice
Posts: 5
Joined: Sat May 09, 2009 4:14 pm

Post by InTeNsO »

Well the problem is, the eggdrop is only on 8.4... so no support with dict.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Actually there is a backport of the Tcl 8.5 [dict] command for Tcl 8.4 that can be found at https://github.com/noyesno/Tcl-Dict

To be honest, haven't tried it as I always update to the latest versions.
Once the game is over, the king and the pawn go back in the same box.
Post Reply