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.

reducing usage of set/variable

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

reducing usage of set/variable

Post by ComputerTech »

Anyway possible to not use variable soo many times, maybe like this.

Code: Select all

foreach {foo bar moo} [split $args] {break}
For this code

Code: Select all

namespace eval CT-IP {

############################
#  Start of configuration  #
#--------------------------#

##########################
# Trigger
##
variable trig "!ip"

##########################
# API 
##
variable api "2bbd882cec5fa5f26d53fe6049a21343"


##########################
# Flags
##
# n = Owner
# m = Master
# o = Op
# h = Halfop
# v = Voice
# f = Friend
# - = Everyone
##
variable flag "-|-"


##########################
# Output
##
# M = Message
# N = Notice
# N = Nick
# C = Channel
##
variable outp "MC"

 
############################
#   End of configuration   #
#--------------------------#

###############################################################################

variable author "ComputerTech"
variable version "v0.1"
variable name "CT-IP"

package require http
package require json

bind PUB $flag $trig [namespace current]::IP:proc

proc putout {text} {
	variable outp
	upvar 1 nick nick chan chan
	switch -- [string index $outp 0] {
		"M" {variable action "PRIVMSG"}
		"N" {variable action "NOTICE"}
	}
	switch -- [string index $outp 1] {
		"N" {variable target $nick}
		"C" {variable target $chan}
	}
	putserv "${action} ${target} :$text"
}

proc IP:proc {nick host hand chan text} {
 variable api
 variable url "http://api.ipstack.com/"
 variable params [::http::formatQuery q [lindex $text 0] $api]
 variable data [http::data [http::geturl "$url?$params" -timeout 10000]]
      variable data2 [::json::json2dict $data]
      http::cleanup $data2
  variable ip [dict get $data2 "ip"]
  variable hostname [dict get $data2 "hostname"]
  variable type [dict get $data2 "type"]
  variable continent [dict get $data2 "continent_name"]
  variable country [dict get $data2 "country_name"]
  variable region [dict get $data2 "region_name"]
  variable city [dict get $data2 "city"]
  variable latitude [dict get $data2 "latitude"]
  variable longitude [dict get $data2 "longitude"]
  variable location [dict get $data2 "location"]
  variable language [dict get $location "languages"] 
  variable name [dict get $language "name"]
putout "\002IP\002: $ip | \002Hostname\002: $hostname | \002Type\002: $type"
putout "\002Continent\002: $continent | \002Country\002: $country | \002Region\002: $region"
putout "\002City\002: $city | \002Latitude\002: $latitude | \002Longitude\002 $longitude |\002Language\002 $name"
  }
putlog "$name $version by $author Loaded!"
}

Imo it looks messy with multiple usage of variable or set
thanks in advanced :P
ComputerTech
User avatar
CrazyCat
Revered One
Posts: 1305
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

You don't want to name all the variables in the dict ?

Code: Select all

foreach varname [dict keys $data2] {
   set ip($varname) [dict get $data2 $varname]
}
And after, use $ip(ip), $ip(hostname), ...
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

That looks perfect CrazyCat, i'll go test it :D
ComputerTech
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

Okay so, i tried it and i get this error

Code: Select all

[00:37:20] Tcl error [::CT-IP::IP:proc]: can't define "ip(ip)": name refers to an element in an array
with this code

Code: Select all


namespace eval CT-IP {

	############################
	#  Start of configuration  #
	#--------------------------#

	##########################
	# Trigger
	##
	variable trig "!ip"

	##########################
	# API
	##
	variable api "2bbd882cec5fa5f26d53fe6049a21343"


	##########################
	# Flags
	##
	# n = Owner
	# m = Master
	# o = Op
	# h = Halfop
	# v = Voice
	# f = Friend
	# - = Everyone
	##
	variable flag "-|-"


	##########################
	# Output
	##
	# M = Message
	# N = Notice
	# N = Nick
	# C = Channel
	##
	variable outp "MC"

	############################
	#   End of configuration   #
	#--------------------------#

	###############################################################################

	variable author "ComputerTech"
	variable version "v0.1"
	variable name "CT-IP"

	package require http
	package require json

	bind PUB $flag $trig [namespace current]::IP:proc

	proc putout {text} {
		variable outp
		upvar 1 nick nick chan chan
		switch -- [string index $outp 0] {
			"M" {variable action "PRIVMSG"}
			"N" {variable action "NOTICE"}
		}
		switch -- [string index $outp 1] {
			"N" {variable target $nick}
			"C" {variable target $chan}
		}
		putserv "${action} ${target} :$text"
	}
	proc IP:proc {nick host hand chan text} {
		variable api
                       variable url "http://api.ipstack.com/[lindex $text 0]?access_key=$api&hostname=1"
		variable data [http::data [http::geturl "$url" -timeout 10000]]
		variable data2 [::json::json2dict $data]
		http::cleanup $data2
                        foreach varname [dict keys $data2] {
                        variable ip($varname) [dict get $data2 $varname]
}
        putout "$ip(ip)"
	}
	putlog "$name $version by $author Loaded!"
}
And here's a example from the json output

Code: Select all

{
  "ip": "2a07:4840::1:0:0:0:2f",
  "hostname": "2a07:4840::1:0:0:0:2f",
  "type": "ipv6",
  "continent_code": "EU",
  "continent_name": "Europe",
  "country_code": "GB",
  "country_name": "United Kingdom",
  "region_code": "ENG",
  "region_name": "England",
  "city": "Maidenhead",
  "zip": "SL6",
  "latitude": 51.531681060791016,
  "longitude": -0.7307500243186951,
  "location": {
    "geoname_id": 2643186,
    "capital": "London",
    "languages": [
      {
        "code": "en",
        "name": "English",
        "native": "English"
      }
    ],
    "country_flag": "http://assets.ipstack.com/flags/gb.svg",
    "country_flag_emoji": "🇬🇧",
    "country_flag_emoji_unicode": "U+1F1EC U+1F1E7",
    "calling_code": "44",
    "is_eu": true
  }
}
ComputerTech
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

He said

Code: Select all

set ip($varname) [dict get $data2 $varname]
and you used:

Code: Select all

variable ip($varname) [dict get $data2 $varname] 
To make it less confusing I would replace varname with key, since that's what you are looping over the keys from that dictionary.
Once the game is over, the king and the pawn go back in the same box.
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

oh damn lol, i missed that :lol:

Thanks caesar :P
ComputerTech
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

Still don't understand why it generates a error in Partyline.

Code: Select all

[01:32:57] Tcl error [::CT-IP::IP:proc]: can't define "ip 2001:bc8:32d7:26c:: hostname 2001:bc8:32d7:26c:: type ipv6 continent_code EU continent_name Europe country_code FR country_name France region_code IDF region_name Île-de-France city Saint-Ouen zip 75008 latitude 48.861000061035156 longitude 2.3380000591278076 location {geoname_id 2977824 capital Paris languages {{code fr name French native Français}} country_flag http://assets.i
With this code

Code: Select all

namespace eval CT-IP {

   ############################
   #  Start of configuration  #
   #--------------------------#

   ##########################
   # Trigger
   ##
   variable trig "!ip"

   ##########################
   # API
   ##
   variable api "2bbd882cec5fa5f26d53fe6049a21343"


   ##########################
   # Flags
   ##
   # n = Owner
   # m = Master
   # o = Op
   # h = Halfop
   # v = Voice
   # f = Friend
   # - = Everyone
   ##
   variable flag "-|-"


   ##########################
   # Output
   ##
   # M = Message
   # N = Notice
   # N = Nick
   # C = Channel
   ##
   variable outp "MC"

   ############################
   #   End of configuration   #
   #--------------------------#

   ###############################################################################

   variable author "ComputerTech"
   variable version "v0.1"
   variable name "CT-IP"

   package require http
   package require json

   bind PUB $flag $trig [namespace current]::IP:proc

   proc putout {text} {
      variable outp
      upvar 1 nick nick chan chan
      switch -- [string index $outp 0] {
         "M" {variable action "PRIVMSG"}
         "N" {variable action "NOTICE"}
      }
      switch -- [string index $outp 1] {
         "N" {variable target $nick}
         "C" {variable target $chan}
      }
      putserv "${action} ${target} :$text"
   }
   proc IP:proc {nick host hand chan text} {
      variable api
                       variable url "http://api.ipstack.com/[lindex $text 0]?access_key=$api&hostname=1"
      variable data [http::data [http::geturl "$url" -timeout 10000]]
      variable data2 [::json::json2dict $data]
      http::cleanup $data2
                        foreach key [dict keys $data2] {
                        set ip($key) [dict get $data2 $key]
}
        putout "$ip(ip)"
   }
   putlog "$name $version by $author Loaded!"
}
:roll:
ComputerTech
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Most likely cos you are doing a cleanup on $data2, instead of $data.. anyway your variables confuses me so I made it from scratch.

Code: Select all

proc IP:proc {nick host hand chan text} {
	variable api
	if {[scan $text {%s} ip] < 1} return
	set url "http://api.ipstack.com/$ip?access_key=$api&hostname=1"
	catch {set http [::http::geturl $url -timeout 6000]} error
	set data [::http::data $http]
	set dict [::json::json2dict $data]
	::http::cleanup $http
	foreach key [dict keys $dict] {
		set result($key) [dict get $dict $key]
	}
	putout "$result(ip)"
}
Once the game is over, the king and the pawn go back in the same box.
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

That works perfect caesar! thanks a ton mate :wink:
ComputerTech
Post Reply