mIRC Homepage
Posted By: sparta hash table problem - 15/11/08 11:23 PM
What am i missing here? the hash table have the words stored in it, i can list them to a dialog, but when i trying to remove or add a word it don't notice the word is in the list. nether on add or remove, it add the word over and over again, and when i trying to remove it, then i get that the word don't exist, so what am i missing here? the %var store the word i want to remove, tested to write in the word in the code, still no luck with it. loadbadword loads the words to the dialog, and as i wrote before it show all the words as it show, so nothing wrong there. and i can echo the words to me:

moo
blaa
test

and i trying to remove "moo", nothing, and i tested all the word's with the same result..

Code:
on *:start:{
  if (!$hget(badword)) { hmake badword }
  if ($exists(badword.hsh)) { hload badword badword.hsh }
}

alias add.bword {
  if ($hget(badword,%badword)) { dialog -t badword The badword $+($chr(34),%badword,$chr(34)) already exists in the list. }
  if (!$hget(badword,%badword)) { hadd badword %badword | dialog -t badword The badword $+($chr(34),%badword,$chr(34)) added succesfully. | loadbadword }
}
alias del.bword {
  if (!$hget(badword,%rbadword)) { dialog -t badword The badword $+($chr(34),%rbadword,$chr(34)) does not exist in the list. }
  if ($hget(badword,%rbadword)) { hdel badword %rbadword | dialog -t badword Badword $+($chr(34),%rbadword,$chr(34)) removed succesfully. | loadbadword }
}
Posted By: Horstl Re: hash table problem - 15/11/08 11:35 PM
if you "/hadd table %badword" (without data), $hget(table,%badword) returns nothing (because it would return the data for item %badword). Check for $hget(table,%badword).item instead
Posted By: sparta Re: hash table problem - 15/11/08 11:39 PM
grr, hate it when i miss something like that frown maybe i sit to long and don't see the "small" errors, when added .item it worked as a charm.. smile thnx for your help.
Posted By: DJ_Sol Re: hash table problem - 15/11/08 11:44 PM
When you add your entry to the hash table you dont give it any data.

hadd badword %badword

So when you check for $hget(badword,%badword) the result is $null or $false because there is no data.

This is why when you try to remove it with your alias the result is ..


if (!$hget(badword,%rbadword)) { dialog -t badword The badword $+($chr(34),%rbadword,$chr(34)) does not exist in the list. }


edit:
lol, looks like I took too long writing that lol. smile
Posted By: Horstl Re: hash table problem - 15/11/08 11:45 PM
Yepp it's the smallest things that may cause the most despair laugh
And the helpfile isn't explicit about this: "$hget(name/N, N).item" - No word about $hget(name/N,item/N).item

Posted By: sparta Re: hash table problem - 15/11/08 11:46 PM
uhum, ok, learn something new every day smile and your right, i have been reading it over and over again, and couldn't figure out the problem. smirk

;------ edit

I don't get it, now i can add 1 word, then if i try add 1 more i get that the word already in the hash table, but it's not. and if i remove the added word, then i can add 1 new and after that the same result, so what's wrong with the code? and yes, the %badword get the new value so it's nothing like that.
Code:
alias add.bword {
  if ($hget(badword,%badword).item) { dialog -t badword The badword $+($chr(34),%badword,$chr(34)) already exists in the list. }
  if (!$hget(badword,%badword).item) { hadd badword %badword | dialog -t badword The badword $+($chr(34),%badword,$chr(34)) added succesfully. | loadbadword }
}
Posted By: RusselB Re: hash table problem - 16/11/08 12:46 AM
You aren't using the full line for the /hadd command

/hadd <table> <item> <data>

Usage
Code:
/add.bword <badword1> <badword2>,<badword3>,<badword4>


For remote usage:
Code:
on *:text:!add.bword*:*:{
  add.bword $2-
}
Code:
alias add.bword {
  var %bad.words = $replace($strip($1-),$chr(44),$chr(32)), %a = 1, %b = $numtok(%bad.words,32)
  while %a <= %b {
    var %badword = $gettok(%bad.words,%a,32)
    if $hget(badword,%badword) {
      dialog -t badword The badword $qt(%badword) already exists in the list.
    }
    else {
      .hadd -m badword %badword $true
      dialog -t badword The badword $qt(%badword) added successfully
    }
    inc %a
  }
}
Posted By: Horstl Re: hash table problem - 16/11/08 01:12 AM
RusselB is right - I should have tested it with more than 1 item blush
$hget(table,foobar).item = $hget(table,0).item
Use the "data" part (added by? date added?), or add at least a "dummy" data value and refer to if $hget(badword,%badword) like in your initial script ... or (if you don't assing a data value), use if $hfind(badword,%badword)
Posted By: RusselB Re: hash table problem - 16/11/08 01:21 AM
Actually, $hget(table,0).item will return the number of items in the table.

Quote:
$hget(name/N, N).item
This allows you to reference the table as an index from 0 to N, in order to look up the Nth item in the table.
If N is zero, returns the total number of items in the table.
You can also reference the Nth data value directly with $hget().data.
Note: This method is provided as a convenience, it is not an efficient way to use the hash table.
Posted By: Horstl Re: hash table problem - 16/11/08 01:28 AM
That's right, and $hget(table,abcd).item is returning the same value (total number of items), not the N of the item abcd if item abcd exists (which I assumed misleadingly because I tested only a single item in a test table).
Posted By: RusselB Re: hash table problem - 16/11/08 03:03 AM
It appears that there isn't an option to return the N portion of $hget(table,N)


Posted By: DJ_Sol Re: hash table problem - 16/11/08 07:52 AM
What about $hfind ...

$hfind(name/N, text, N, M)

Searches table for the Nth item name which matches text. Returns item name.
Posted By: Buggs2008 Re: hash table problem - 16/11/08 08:56 AM
Hello,

Something I made a while back ago.

This one uses the hfind like DJ Sol had mentioned

Code:
;==================================================================================================================================
; PROFANITY PROTECTION
;==================================================================================================================================
dialog pfp {
  title "Profanity Protection"
  size -1 -1 118 126
  option dbu
  combo 1, 6 9 70 97, sort size vsbar
  button "Add", 2, 80 21 32 11, default flat
  button "Delete", 3, 80 33 32 11, flat
  box "Profanity Protection", 5, 1 0 116 109
  button "Delete All", 7, 80 45 32 11, flat
  button "Import", 8, 80 69 32 11, flat
  button "Export", 9, 80 81 32 11, flat
  button "Enable", 11, 80 93 32 11, flat
  button "OK", 4, 26 112 32 11, flat ok
  button "Cancel", 6, 62 112 32 11, flat cancel
  button "Refresh", 10, 80 9 32 11, flat
  button "Search", 12, 80 57 32 11, flat
}

#pfp off
on *:EXIT:{ if ($hget(badword)) { hsave badword $+($scriptdir,badword.hsh) } }
on *:DISCONNECT:{ if ($hget(badword)) { hsave badword $+($scriptdir,badword.hsh) } }

on *:TEXT:*:#: {
  if ($nick !isop #) && ($hfind(badword,*,0,w) > 0) {
    var %i = $hfind(badword,*,0,w) | while (%i) {
      if ($hfind(badword,*,%i,w) isin $1-) { 

        hinc -m Cuss $nick 1 | var %w = $+($mid($hfind(badword,*,%i,w),1,1),$str($chr(42),$calc($len($hfind(badword,*,%i,w)) - 1)))
        if ($hget(Cuss,$nick) == 1) { msg # $nick $+ , this is your $ord($hget(Cuss,$nick)) first warining for using profanity! Please refrain from using the word: %w Thank you.  }
        elseif ($hget(Cuss,$nick) == 2) { msg # $nick $+ , Please consider this is your $ord($hget(Cuss,$nick)) warning for using profanity. A repeat violation will result in you being removed from this chatroom following a 1 hour ban. }
        elseif ($hget(Cuss,$nick) >= 3) { kick # $nick Banned: For the using the word %w This was your $ord($hget(Cuss,$nick)) offense (Access ban set for 1 hour) | hfree Cuss }

      }
      dec %i
    }
  }
}
#pfp end


; -- ALIASES
alias pfp { dialog -m pfp pfp }
alias rem.word {
  var %f = $did($1,$2).lines
  while (%f >= 1) {
    if ($gettok($did($1,$2,%f),1,32) == $3) return %f
    dec %f 
  }
  else return 0
}

alias ref_words {
  did -r $1 1 | var %i = 1
  while (%i <= $hfind(badword,*,0,w)) {
    did -a $1 1 $hfind(badword,*,%i,w) 
    inc %i
  }
  hsave badword $+($scriptdir,badword.hsh)
}



on *:dialog:pfp:init:*: {
  if (!$hget(badword)) { hmake badword }
  if ($exists($+($scriptdir,badword.hsh))) { hload badword $scriptdir $+ badword.hsh }

  ; -- Load all profane words upon initiallization
  if ($group(#pfp) == on) { did -ra $dname 11 Disable }
  else { did -ra $dname 11 Enable }

  if ($hfind(badword,*,0,w) > 0) { var %i = 1 | while (%i <= $hfind(badword,*,0,w)) { did -a $dname 1 $hfind(badword,*,%i,w) | inc %i  } }


}


; -- Add Profane Word ID: 2
; -- Also checks to see that there is no duplication of an entry.
on *:dialog:pfp:sclick:2: { 
  var %w = $did($dname,1)
  if (!$hget(badword,%w)) { hadd badword %w | did -a $dname 1 %w | $ref_words($dname) }
}

; -- Delete a profane word ID: 3
; -- Also checks to see that the word is present before deletion.
on *:dialog:pfp:sclick:3: {
  var %w = $did($dname,1).seltext, %l = $did($dname,1).sel
  echo -a * %w Deleted! | hdel badword %w | did -d $dname 1 %l | $ref_words($dname)
}
; -- Delete All ID: 7
on *:dialog:pfp:sclick:7: { 
  if ($hget(badword)) { hfree badword }
  did -r $dname 1 | hmake badword | hsave badword badword.hsh | $ref_words($dname)
}
; -- Import Feature ID: 8
on *:dialog:pfp:sclick:8: { 
  hload badword $sfile($scriptdir $+ badword.hsh,Select a file to import,OK) | $ref_words($dname)
}

; -- Export Feature ID: 9
on *:dialog:pfp:sclick:9: {
  hsave badword $sfile($scriptdir $+ badword.hsh,Export List,Save)
}
; -- Refresh Profanity Word List ID: 10
on *:dialog:pfp:sclick:10: { $ref_words($dname) }

; -- Enable/Disable Feature ID: 11
on *:dialog:pfp:sclick:11: { 

  if ($did(11) == Enable) { .enable #pfp | did -ra $dname 11 Disable }
  else { .disable #pfp | did -ra $dname 11 Enable }

}

; -- Search Feature ID: 12
on *:dialog:pfp:sclick:12: {
  var %w = $$?="Enter a keyword to search for:"
  did -c $dname 1 $didwm($dname,1,%w)
}
 
Posted By: RusselB Re: hash table problem - 16/11/08 02:40 PM
As you stated, $hfind returns the item name, not the corresponding numeric.

Eg:
Code:
/hadd -m Name temp temp
/hadd -m Name test2 test2
/hadd -m Name text text

//echo -a $hget(Name,).item  -> returns 3
//echo -a $hfind(Name,text,0) -> returns 1
//echo -a $hget(Name,1) -> returns temp
//echo -a $hfind(Name,text,1) -> returns text

$hget(Name,text).N -> would return 3 (if this function existed)

Personally I don't see a real use for a function like that, so I'm not going to put it as a Feature suggestion.
Posted By: Wims Re: hash table problem - 16/11/08 04:22 PM
Quote:
$hget(Name,text).N -> would return 3 (if it existed)


This is wrong, when using hash table, you should not take care about the N position of an item in the table, that's not how hash table should be used.
And $hget(table,1).item always returns the last added item, so $hget(Name,text).N would return 1.
If you're (the OP) adding an item with no data, use $hfind as said by Horstl
Posted By: RusselB Re: hash table problem - 16/11/08 04:39 PM
I used bad phraseology in the condition in my earlier post regarding $hget(table,item).N and didn't realize it until after your post.
I have edited my post to make what I was talking about clearer.
Posted By: Wims Re: hash table problem - 16/11/08 05:00 PM
Lol, even if it's more clearer now, I already understood, it's just that as i said $hget(table,1).item always returns the last added item, so $hget(Name,text).N would return 1 in your exemple.

And while i'm here, in your exemple, $hget(Name,).item return $null and with $hfind(Name,text,0), you're searching for an exact item match, so it will always be 1 and again $hget(Name,1) is $null.
Posted By: RusselB Re: hash table problem - 16/11/08 05:17 PM
As it currently works, yes,
Quote:
$hget(Name,text).N would return 1 in your exemple.
, however, I think you partially misunderstood what I was trying to refer to.

$hget(table,N).item returns the item name

What I was, somewhat, suggesting, is something like
$hget(table,item).N where N (as used in $hget(table,N)) is returned.
Posted By: Wims Re: hash table problem - 16/11/08 05:49 PM
I perfectly understood what you was talking about smile i'm just saying when using hash table, know the N position ($hget(table,n).item) in the table is useless.
Posted By: sparta Re: hash table problem - 17/11/08 12:53 PM
Replying to no one, just have a question.

i save the information to the file.hsh when i disconnect from the server, but can i save the information on exit too? when i code im not connected to a server (and if i configure the script without connecting i need to redo everything again if i didn't connect before closing mirc), i asking cos i don't know how long time the information is stored in the table after exit, and my computer maybe save the information fast enough to save it in the file, while you have a slower computer it maybe don't get the information in time.
Posted By: starbucks_mafia Re: hash table problem - 17/11/08 12:57 PM
The speed of your computer is irrelevant. If you have an on exit event mIRC will not close until that event has finished processing. So if you use /hsave inside the event it won't close until the hash table has finished being written to the file.
Posted By: DJ_Sol Re: hash table problem - 17/11/08 01:03 PM
Well that may be true, it can hardly be counted on that $hget(table,1).item is always going to return the exact item name you are looking for. It is not solid enough to base your code on. I require solutions that are 100% accurate, not just accurate if some conditions happen to be met.
Posted By: DJ_Sol Re: hash table problem - 17/11/08 01:07 PM
Yes save on disconnect, on exit and on me:*:quit:{.
Posted By: Wims Re: hash table problem - 17/11/08 01:38 PM
What ? I'm only saying that $hget(name,1).item will always return the last added item, I never said it will return the item name you're looking for
Posted By: sparta Re: hash table problem - 17/11/08 02:12 PM
Thank you smile instead of start a new post i ask here wink i know goto aren't the best to use, but what if i have a part of a alias i want to be triggered if the nick is in a list. What else could i use?
Code:
alias test {
  if (%var >= 3) && (%f != 1) { do stuff }

  ;------ skip everything down to start if $cexcctcp = $true
  if ($cexcctcp == $true) { goto start }
  ;------ skip everything down to start if $cexcctcp = $true

  if (%f == 1) { return }
  if ($readini(test.ini) == Disabled) { do stuff }
  elseif ($readini(test.ini) == High) { do stuff }
  elseif ($readini(test.ini) == Normal) { do stuff }
  :start
  echo -a rest of code here
}
Posted By: starbucks_mafia Re: hash table problem - 17/11/08 02:37 PM
Code:
if $cexcctcp != $true {
  ; Code that was before :start
}
; Code after :start


There really wasn't any need to ask that here. You should at least try and figure things out for yourself.
Posted By: sparta Re: hash table problem - 17/11/08 06:45 PM
well, didn't work now, and didn't work when i asked. so that wont do what i asked..
Posted By: starbucks_mafia Re: hash table problem - 17/11/08 07:14 PM
Was your question at all related to the code you gave and comments it includes? If not then you need to specify what "list" you're referring to, and either way the answer is the same: use an if statement if a peice of code should only be executed when a certain condition is met.
Posted By: sparta Re: hash table problem - 17/11/08 09:08 PM
This is how the code looks like:
Code:
alias test {
  if ($cexcctcp == $true) { goto start }
  if ($readini(settings.ini,protection,ctcp) == Disabled) { goto start }
  inc -u5 $+(%, no., $nick) | if ($($+(%, no., $nick), 2) >= 3) { echo -a CTCP flood detected from: $+($nick,$chr(44)) %ctcpflood ctcp's in %ctcpftime seconds. $+($chr(40),ignored,$chr(41)) | return }
  :start
  echo -> rest of the code here
}

Notice i need it to "goto start" if $cexcctcp = $true, and if the $readini(settings.ini,protection,ctcp) = disabled, i removed some code and added some, so this don't match the one i pasted befor, and i did try the code you suggested, but had no luck with it then, and no luck with in on this one ether so then i asked.
Posted By: RusselB Re: hash table problem - 17/11/08 10:21 PM
Code:
alias test {
  inc -u5 $+(%, no., $nick)
  if ($($+(%, no., $nick), 2) >= 3) {
    echo -a CTCP flood detected from: $+($nick,$chr(44)) %ctcpflood ctcp's in %ctcpftime seconds. $+($chr(40),ignored,$chr(41))
  }
  if ($cexcctcp == $true) || ($readini(settings.ini,protection,ctcp) == Disabled) { 
    echo -> rest of the code here
  }
}


Simple matter of placing your commands/if statements in a slightly different order.
Posted By: Tano Re: hash table problem - 13/04/18 08:32 AM
Here in bad.word where is the code to give ban ?
© mIRC Discussion Forums