mIRC Homepage

hash question

Posted By: pouncer

hash question - 11/05/07 12:27 AM

If I was to add to a hash table like thos

hadd -m image.1_NUMBER_0_PIXEL_2_0 2_0 $false
hadd -m image.1_NUMBER_0_PIXEL_3_0 3_0 $true
hadd -m image.1_NUMBER_0_PIXEL_1_1 1_1 $false
hadd -m image.1_NUMBER_0_PIXEL_4_1 4_1 $false

How can I then get the total number of hash entries that match table:
image.1_NUMBER_0_*
..that are set to $true?

in the above case, it should be 1
Posted By: Riamus2

Re: hash question - 11/05/07 12:58 AM

You'd have to do a loop with $hfind and $hget. Use $hfind with wildcards to find the item names and loop through from 1 to N and $hget the result of each to see if it's $true.

Code:
var %cnt = 1, %total = $hfind(table,image.1_NUMBER_0_*,0,w)
while (%cnt <= %total) {
  if ($hget(table,$hfind(table,image.1_NUMBER_0_*,%cnt,w)) == $true) {
    inc %true
  }
  inc %cnt
}
echo -a True: %true


Just change what you need to (table name, etc).

Below is a more verbose version that should make it easy to see how it works. I wouldn't suggest using the code below. It's just here to help explain how it works.

Code:
var %cnt = 1
var %item.search = image.1_NUMBER_0_*
var %total = $hfind(table,%item.search,0,w)
while (%cnt <= %total) {
  var %item.name = $hfind(table,%item.search,%cnt,w)
  if ($hget(table,%item.name) == $true) {
    inc %true
  }
  inc %cnt
}
echo -a True: %true

Posted By: pouncer

Re: hash question - 11/05/07 01:07 AM

Thanks mate. what about something like this:

hadd -m @Number.1 0 6
hadd -m @Number.1 1 3
hadd -m @Number.1 2 4
hadd -m @Number.1 3 4
hadd -m @Number.1 4 20
hadd -m @Number.1 5 5
hadd -m @Number.1 6 7
hadd -m @Number.1 7 5
hadd -m @Number.1 8 5
hadd -m @Number.1 9 4

I want to pick out the table name/data which contains the hightest number which is @Number.1 4

Is there a way to grab this info without using loops?
Posted By: Riamus2

Re: hash question - 11/05/07 01:11 AM

Save it to a file or to a custom window with line numbers and use /filter, then use $hget on the line number to get the right item.

See this thread (post by DaveC) for more information on sorting hash tables.
Posted By: RusselB

Re: hash question - 11/05/07 01:12 AM

Without using a loop, save the hash table to a file, then use /filter on the file.

Personally, I'd prefer to use a loop for something like this, as the results would be almost instantaneous, especially with a table that only has 10 items in it
Posted By: pouncer

Re: hash question - 11/05/07 01:20 AM

Ahhh, cheers bud.

I'll give it a read!
Posted By: pouncer

Re: hash question - 11/05/07 01:20 PM

hey riamus i got a small problem, this time i got 2 tables:

hadd -m @Number.1 0 6
hadd -m @Number.1 1 3
hadd -m @Number.1 2 4
hadd -m @Number.1 3 4
hadd -m @Number.1 4 20
hadd -m @Number.1 5 5
hadd -m @Number.1 6 7
hadd -m @Number.1 7 5
hadd -m @Number.1 8 5
hadd -m @Number.1 9 4

hadd -m @Number.2 0 3
hadd -m @Number.2 1 16
hadd -m @Number.2 2 7
hadd -m @Number.2 3 6
hadd -m @Number.2 4 3
hadd -m @Number.2 5 6
hadd -m @Number.2 6 6
hadd -m @Number.2 7 5
hadd -m @Number.2 8 2
hadd -m @Number.2 9 5

again for @Number.1 it should return '4' as it has highest value of 20.

for @Number.2 it should return 1 as it has highest value 16.

i couldnt get this working with tha filter thing, any ideas guys?
Posted By: Riamus2

Re: hash question - 11/05/07 01:43 PM

Don't forget with the filter method that you need to sort based on the second "word" in your data, not the first. And you want to sort with a numeric sort.

Display the code you have.
Posted By: pouncer

Re: hash question - 11/05/07 09:27 PM

Code:
alias ts {
  hfree -w *Number*

  hadd -m @Number.1 0 6
  hadd -m @Number.1 1 3
  hadd -m @Number.1 2 4
  hadd -m @Number.1 3 4
  hadd -m @Number.1 4 20
  hadd -m @Number.1 5 5
  hadd -m @Number.1 6 7
  hadd -m @Number.1 7 5
  hadd -m @Number.1 8 5
  hadd -m @Number.1 9 4

  hadd -m @Number.2 0 3
  hadd -m @Number.2 1 16
  hadd -m @Number.2 2 7
  hadd -m @Number.2 3 6
  hadd -m @Number.2 4 3
  hadd -m @Number.2 5 6
  hadd -m @Number.2 6 6
  hadd -m @Number.2 7 5
  hadd -m @Number.2 8 2
  hadd -m @Number.2 9 5

  hsave -no @Number.1 tempfile.txt
  hsave -no @Number.2 tempfile.txt
  filter -ffcteun 1 32 tempfile.txt tempfile.txt
}


the tempfile.txt reads like this:

Code:
9 16
10 7
1 6
3 6
4 6
5 5
7 5
2 3
8 3
6 2


:s

All I'm trying to do is capture the number '4' and '1' as they have the highest values in @number.1 and @number.2 respectively
Posted By: RusselB

Re: hash question - 11/05/07 11:29 PM

You'll have to save each hash table and filter the saved file, one at a time or use different names for the input &/or output of the /filter command, otherwise you're going to get incorrect results.
Posted By: deegee

Re: hash question - 14/05/07 01:36 AM

If there's not too many entries in the tables, it may be quicker/easier to loop the table items.
Code:
; /gethigh <Table> (/gethigh @Number.1)
alias gethigh {
  var %i = 1,%a = 0,%b
  while $hget($$1,%i).data {
    if $v1 > %a { var %a = $v1,%b = %i }
    inc %i
  }
  echo -a $1 $hget($1,%b).item == %a
}
Posted By: RusselB

Re: hash question - 14/05/07 02:20 AM

Please ensure that you read all of the posts before posting, as the OP stated in their second post
Quote:
Is there a way to grab this info without using loops?

© 2020 mIRC Discussion Forums