egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Open File, Search for Text, Output Text

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
InTeNsO
Voice


Joined: 09 May 2009
Posts: 5

PostPosted: Mon Jun 23, 2014 4:57 am    Post subject: Open File, Search for Text, Output Text Reply with quote

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:
,{"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:
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.
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Mon Jun 23, 2014 7:36 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
InTeNsO
Voice


Joined: 09 May 2009
Posts: 5

PostPosted: Tue Jun 24, 2014 5:19 pm    Post subject: Reply with quote

Well the problem is, the eggdrop is only on 8.4... so no support with dict.
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Wed Jun 25, 2014 1:05 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber