mIRC Home    About    Download    Register    News    Help

Page 1 of 2 1 2 >
Topic Options
#206464 - 15/11/08 11:23 PM hash table problem
sparta Offline
Planetary brain

Registered: 22/02/03
Posts: 3432
Loc: Sweden/Borlänge
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 }
}
_________________________
if ($me != tired) { return } | else { echo -a Get a pot of coffee now $+($me,.) }

Top
#206465 - 15/11/08 11:35 PM Re: hash table problem [Re: sparta]
Horstl Offline
Hoopy frood

Registered: 03/11/06
Posts: 1559
Loc: Germany
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

Top
#206466 - 15/11/08 11:39 PM Re: hash table problem [Re: Horstl]
sparta Offline
Planetary brain

Registered: 22/02/03
Posts: 3432
Loc: Sweden/Borlänge
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.
_________________________
if ($me != tired) { return } | else { echo -a Get a pot of coffee now $+($me,.) }

Top
#206467 - 15/11/08 11:44 PM Re: hash table problem [Re: sparta]
DJ_Sol Offline
Hoopy frood

Registered: 04/01/07
Posts: 1156
Loc: Seattle
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


Edited by DJ_Sol (15/11/08 11:46 PM)

Top
#206468 - 15/11/08 11:45 PM Re: hash table problem [Re: sparta]
Horstl Offline
Hoopy frood

Registered: 03/11/06
Posts: 1559
Loc: Germany
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


Top
#206469 - 15/11/08 11:46 PM Re: hash table problem [Re: DJ_Sol]
sparta Offline
Planetary brain

Registered: 22/02/03
Posts: 3432
Loc: Sweden/Borlänge
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 }
}


Edited by sparta (16/11/08 12:18 AM)
_________________________
if ($me != tired) { return } | else { echo -a Get a pot of coffee now $+($me,.) }

Top
#206470 - 16/11/08 12:46 AM Re: hash table problem [Re: sparta]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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
  }
}

Top
#206471 - 16/11/08 01:12 AM Re: hash table problem [Re: sparta]
Horstl Offline
Hoopy frood

Registered: 03/11/06
Posts: 1559
Loc: Germany
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)


Edited by Horstl (16/11/08 01:35 AM)

Top
#206472 - 16/11/08 01:21 AM Re: hash table problem [Re: Horstl]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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.

Top
#206473 - 16/11/08 01:28 AM Re: hash table problem [Re: RusselB]
Horstl Offline
Hoopy frood

Registered: 03/11/06
Posts: 1559
Loc: Germany
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).

Top
#206475 - 16/11/08 03:03 AM Re: hash table problem [Re: Horstl]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
It appears that there isn't an option to return the N portion of $hget(table,N)



Top
#206486 - 16/11/08 07:52 AM Re: hash table problem [Re: RusselB]
DJ_Sol Offline
Hoopy frood

Registered: 04/01/07
Posts: 1156
Loc: Seattle
What about $hfind ...

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

Searches table for the Nth item name which matches text. Returns item name.

Top
#206489 - 16/11/08 08:56 AM Re: hash table problem [Re: sparta]
Buggs2008 Offline
Fjord artisan

Registered: 16/10/07
Posts: 214
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)
}
 

Top
#206493 - 16/11/08 02:40 PM Re: hash table problem [Re: DJ_Sol]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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.


Edited by RusselB (16/11/08 04:34 PM)

Top
#206498 - 16/11/08 04:22 PM Re: hash table problem [Re: RusselB]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3475
Loc: France
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


Edited by Wims (16/11/08 04:22 PM)
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#206499 - 16/11/08 04:39 PM Re: hash table problem [Re: Wims]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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.

Top
#206500 - 16/11/08 05:00 PM Re: hash table problem [Re: RusselB]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3475
Loc: France
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.
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#206502 - 16/11/08 05:17 PM Re: hash table problem [Re: Wims]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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.

Top
#206505 - 16/11/08 05:49 PM Re: hash table problem [Re: RusselB]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3475
Loc: France
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.
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#206546 - 17/11/08 12:53 PM Re: hash table problem [Re: Wims]
sparta Offline
Planetary brain

Registered: 22/02/03
Posts: 3432
Loc: Sweden/Borlänge
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.
_________________________
if ($me != tired) { return } | else { echo -a Get a pot of coffee now $+($me,.) }

Top
Page 1 of 2 1 2 >