[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Ron Green

9/9/2007 6:27:00 PM

What is the purpose of string hash? What would you use it for?
EXAMPLE: "This is a test.".hash RETURNS -649841898. WHAT EXACTLY DOES
THIS REPRESENT? WHAT IS IT GOOD FOR? COMPARISONS?
--
Posted via http://www.ruby-....

19 Answers

Phlip

9/9/2007 6:35:00 PM

0

Ron Green wrote:

> What is the purpose of string hash? What would you use it for?
> EXAMPLE: "This is a test.".hash RETURNS -649841898. WHAT EXACTLY DOES
> THIS REPRESENT? WHAT IS IT GOOD FOR? COMPARISONS?

Run "another test".hash - you get a different number.

It's good for hashes; look them up in any "data structures" textbook. It's a
unique number useful for rapidly accessing that string.

And please DON'T SCREAM!

--
Phlip
http://www.oreilly.com/catalog/9780...
"Test Driven Ajax (on Rails)"
assert_xpath, assert_javascript, & assert_ajax


Marcel Molina Jr.

9/9/2007 6:48:00 PM

0

On Sep 9, 2007, at 1:34 PM, "Phlip" <phlip2005@gmail.com> wrote:

> Ron Green wrote:
>
>> What is the purpose of string hash? What would you use it for?
>> EXAMPLE: "This is a test.".hash RETURNS -649841898. WHAT EXACTLY DOES
>> THIS REPRESENT? WHAT IS IT GOOD FOR? COMPARISONS?
>
> Run "another test".hash - you get a different number.
>
> It's good for hashes; look them up in any "data structures"
> textbook. It's a unique number useful for rapidly accessing that

It should be noted though that String#hash isn't garaunteed to be
unique.

Ron Green

9/9/2007 7:00:00 PM

0

Marcel Molina Jr. wrote:
> On Sep 9, 2007, at 1:34 PM, "Phlip" <phlip2005@gmail.com> wrote:
>
>> Ron Green wrote:
>>
>>> What is the purpose of string hash? What would you use it for?
>>> EXAMPLE: "This is a test.".hash RETURNS -649841898. WHAT EXACTLY DOES
>>> THIS REPRESENT? WHAT IS IT GOOD FOR? COMPARISONS?
>>
>> Run "another test".hash - you get a different number.
>>
>> It's good for hashes; look them up in any "data structures"
>> textbook. It's a unique number useful for rapidly accessing that
>
> It should be noted though that String#hash isn't garaunteed to be
> unique.

Then,again I ask, what is it good for?
--
Posted via http://www.ruby-....

Phlip

9/9/2007 7:08:00 PM

0

Ron Green wrote:

> > It should be noted though that String#hash isn't garaunteed to be
> > unique.
>
> Then,again I ask, what is it good for?

Then I answer again: A tutorial on data structures tells how to use them.

(Maybe I shouldn't post when I'm having a bad day, huh?;)

--
Phlip
http://www.oreilly.com/catalog/9780...
^ assert_xpath
http://tinyurl.... <-- assert_latest Model

Ron Green

9/9/2007 7:24:00 PM

0

Peter Cooper wrote:
> On 9/9/07, Ron Green <rongreen1@mac.com> wrote:
>>
>> Marcel Molina Jr. wrote:
>> > It should be noted though that String#hash isn't garaunteed to be
>> > unique.
>>
>> Then,again I ask, what is it good for?
>
>
> It's still useful as a hash. Marcel wasn't wrong, but *no* fixed size
> hash
> is "guaranteed" to be unique as that's absolutely impossible, per the
> pigeonhole principle
> (http://en.wikipedia.org/wiki/Pigeonhole...).
> String#hash's hash is of a far lower "quality" than that offered by,
> say,
> SHA-1 or SHA-2.
>
> Regards,
> Peter Cooper
> http://www.rubyi...

Peter,
If Its not guaranteed to be unique, then it can't be used for identity.
Can you give me an example of how i would use string.hash?
--
Posted via http://www.ruby-....

Marcin Raczkowski

9/9/2007 7:31:00 PM

0

Ron Green wrote:
> Marcel Molina Jr. wrote:
>
>> On Sep 9, 2007, at 1:34 PM, "Phlip" <phlip2005@gmail.com> wrote:
>>
>>
>>> Ron Green wrote:
>>>
>>>
>>>> What is the purpose of string hash? What would you use it for?
>>>> EXAMPLE: "This is a test.".hash RETURNS -649841898. WHAT EXACTLY DOES
>>>> THIS REPRESENT? WHAT IS IT GOOD FOR? COMPARISONS?
>>>>
>>> Run "another test".hash - you get a different number.
>>>
>>> It's good for hashes; look them up in any "data structures"
>>> textbook. It's a unique number useful for rapidly accessing that
>>>
>> It should be noted though that String#hash isn't garaunteed to be
>> unique.
>>
>
> Then,again I ask, what is it good for?
>
It's for internal use, it's used in Hash to make accessing and finding
strings in hash faster

Konrad Meyer

9/9/2007 7:35:00 PM

0

On Sunday 09 September 2007 12:23:43 pm Ron Green wrote:
> Peter Cooper wrote:
> > On 9/9/07, Ron Green <rongreen1@mac.com> wrote:
> >>
> >> Marcel Molina Jr. wrote:
> >> > It should be noted though that String#hash isn't garaunteed to be
> >> > unique.
> >>
> >> Then,again I ask, what is it good for?
> >
> >
> > It's still useful as a hash. Marcel wasn't wrong, but *no* fixed size
> > hash
> > is "guaranteed" to be unique as that's absolutely impossible, per the
> > pigeonhole principle
> > (http://en.wikipedia.org/wiki/Pigeonhole...).
> > String#hash's hash is of a far lower "quality" than that offered by,
> > say,
> > SHA-1 or SHA-2.
> >
> > Regards,
> > Peter Cooper
> > http://www.rubyi...
>
> Peter,
> If Its not guaranteed to be unique, then it can't be used for identity.
> Can you give me an example of how i would use string.hash?

Let's put it this way. MD5 and SHA-* hashes aren't *guaranteed* to be
unique either. There's just many more cases where strings will share a hash
with String#hash as opposed to something like MD5/SHA-*.

Hashes are useful for identify strings in hashtables. You use this every
time you say something like:

foo = {"bar" => "baz"}
foo["bar"] # => "baz"

HTH,
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Alex Young

9/9/2007 7:37:00 PM

0

Ron Green wrote:
> Peter Cooper wrote:
>> On 9/9/07, Ron Green <rongreen1@mac.com> wrote:
>>> Marcel Molina Jr. wrote:
>>>> It should be noted though that String#hash isn't garaunteed to be
>>>> unique.
>>> Then,again I ask, what is it good for?
>>
>> It's still useful as a hash. Marcel wasn't wrong, but *no* fixed size
>> hash
>> is "guaranteed" to be unique as that's absolutely impossible, per the
>> pigeonhole principle
>> (http://en.wikipedia.org/wiki/Pigeonhole...).
>> String#hash's hash is of a far lower "quality" than that offered by,
>> say,
>> SHA-1 or SHA-2.
>>
>> Regards,
>> Peter Cooper
>> http://www.rubyi...
>
> Peter,
> If Its not guaranteed to be unique, then it can't be used for identity.
> Can you give me an example of how i would use string.hash?
In general, you wouldn't use String#hash, although you might conceivably
want to override it. It's there for Hash. From the documentation on
Object#hash:

"Generates a Fixnum hash value for this object. This function must have
the property that a.eql?(b) implies a.hash == b.hash. The hash value is
used by class Hash."

Note the direction of implication: a == b => a.hash == b.hash, not
a.hash == b.hash => a == b.

--
Alex

Ron Green

9/9/2007 7:49:00 PM

0

Alex Young wrote:
> Ron Green wrote:
>>> pigeonhole principle
>> If Its not guaranteed to be unique, then it can't be used for identity.
>> Can you give me an example of how i would use string.hash?
> In general, you wouldn't use String#hash, although you might conceivably
> want to override it. It's there for Hash. From the documentation on
> Object#hash:
>
> "Generates a Fixnum hash value for this object. This function must have
> the property that a.eql?(b) implies a.hash == b.hash. The hash value is
> used by class Hash."
>
> Note the direction of implication: a == b => a.hash == b.hash, not
> a.hash == b.hash => a == b.

I think I understand. In other words it's not something I would use
directly. I just ran across it in Peter's book and wanted to make sure I
understood. Thanks everybody. Sorry if my ignorance pissed you off
Philip.
--
Posted via http://www.ruby-....

Bill Kelly

9/9/2007 7:57:00 PM

0


From: "Ron Green" <rongreen1@mac.com>
>
> I think I understand. In other words it's not something I would use
> directly. I just ran across it in Peter's book and wanted to make sure I
> understood.

More info:

http://en.wikipedia.org/wiki/Has...
http://en.wikipedia.org/wiki/...


Regards,

Bill