assigning hash to a scalar
------_=_NextPart_001_01CBEB88.EBCAD454
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Hi All
I have following set of code (just assign a hash to a scalar) :
chomp($input =3D <STDIN>);
%input =3D split (/\s+/,$input);
$var =3D %input;
Print "value: : $var\n";
Input : orange 12 apple 23 pinnaple 78
Output : 3/8 --> What does this output mean ?
Thanks
Sunita
------_=_NextPart_001_01CBEB88.EBCAD454--
Re: assigning hash to a scalar
Hi Sunita,
> $var =3D %input;
> =E2=80=A6
> Output : 3/8 --> What does this output mean ?
You are evaluating a hash in a scalar context.
Quoting perldata:
If you evaluate a hash in scalar context, it returns false if the hash
is empty. If there are any key/value pairs, it returns true; more
precisely, the value returned is a string consisting of the number of
used buckets and the number of allocated buckets, separated by a slash.
This is pretty much useful only to find out whether Perl's internal
hashing algorithm is performing poorly on your data set. For example,
you stick 10,000 things in a hash, but evaluating %HASH in scalar
context reveals "1/16", which means only one out of sixteen buckets has
been touched, and presumably contains all 10,000 of your items. This
isn't supposed to happen. If a tied hash is evaluated in scalar
context, a fatal error will result, since this bucket usage information
is currently not available for tied hashes.
Regards,
Alan Haggai Alavi.
--
The difference makes the difference
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
RE: assigning hash to a scalar
Thanks Alan . I had got this piece of info from google but I do not
understand clearly what it wants to define . It would be good , if you
can explain bit more .
Regards
Sunita
-----Original Message-----
From: Alan Haggai Alavi [mailto:alanhaggai [at] alanhaggai.org]
Sent: Saturday, March 26, 2011 1:19 PM
To: Sunita Rani Pradhan
Cc: beginners [at] perl.org
Subject: Re: assigning hash to a scalar
Hi Sunita,
> $var =3D %input;
> ...
> Output : 3/8 --> What does this output mean ?
You are evaluating a hash in a scalar context.
Quoting perldata:
If you evaluate a hash in scalar context, it returns false if the hash
is empty. If there are any key/value pairs, it returns true; more
precisely, the value returned is a string consisting of the number of
used buckets and the number of allocated buckets, separated by a slash.
This is pretty much useful only to find out whether Perl's internal
hashing algorithm is performing poorly on your data set. For example,
you stick 10,000 things in a hash, but evaluating %HASH in scalar
context reveals "1/16", which means only one out of sixteen buckets has
been touched, and presumably contains all 10,000 of your items. This
isn't supposed to happen. If a tied hash is evaluated in scalar
context, a fatal error will result, since this bucket usage information
is currently not available for tied hashes.
Regards,
Alan Haggai Alavi.
--
The difference makes the difference
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: assigning hash to a scalar
Hi Sunita,
> Thanks Alan . I had got this piece of info from google but I do not
> understand clearly what it wants to define . It would be good , if
> you can explain bit more .
You are welcome.
The numbers are dependent on the internal hashing algorithm used by perl
of which I am unaware of.
Hopefully the article: "How Hashes Really Work" can help you understand
better.
- http://www.perl.com/pub/2002/10/01/hashes.html
Regards,
Alan Haggai Alavi.
--
The difference makes the difference
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: assigning hash to a scalar
On Sat, Mar 26, 2011 at 03:53, Sunita Rani Pradhan
<Sunita.Pradhan [at] altair.com> wrote:
> Thanks Alan . I had got this piece of info from google but I do not
> understand clearly what it wants to define . It would be good =C2=A0, if =
you
> can explain bit more .
snip
If you want to know how many key/value pairs are in a hash, you can say
my $count =3D keys %hash;
If you want a reference to %hash, you can say
my $ref =3D \%hash;
Saying
my $hash_info =3D %hash;
Will get you either 0 (if the hash is empty) or the ratio of used to
total buckets. This information is almost, but not completely,
useless to you. To understand what that means you must first
understand how hashing works.
Lets implement a hash using Perl 5. The first thing we need is a
hashing function. Hashing functions turn strings into, hopefully,
unique numbers. Examples of real strong hashing functions are
[MD5][0] or [SHA1][1], but they tend to be too slow for common use, so
people tend to use weaker (i.e. ones that produce less unique output)
functions for hash tables. Perl 5 uses Bob Jenkins [one-at-a-time]
algorithm, which has a nice tradeoff of uniqueness to speed. For our
example, I will use a very weak hashing function:
#!/usr/bin/perl
use strict;
use warnings;
sub weak_hash {
my $key =3D shift;
my $hash =3D 1;
#multiply every character in the string's ASCII/Unicode value toget=
her
for my $character (split //, $key) {
$hash *=3D ord $character;
}
return $hash;
}
for my $string (qw/cat dog hat/) {
print "$string hashes to ", weak_hash($string), "\n";
}
Because hashing functions tend to give back number that are far apart,
you usually use [modulo][3] to reduce the range of numbers it gives
back:
#!/usr/bin/perl
use strict;
use warnings;
sub weak_hash {
my $key =3D shift;
my $hash =3D 1;
#multiply every character in the string's ASCII/Unicode value toget=
her
for my $character (split //, $key) {
$hash *=3D ord $character;
}
return $hash;
}
for my $string (qw/cat dog hat/) {
# the % operator is constraining the number
# weak_hash returns to 0 - 10
print "$string hashes to ", weak_hash($string) % 11, "\n";
}
Now that we have a hashing function, we need somewhere to save the key
and value. This is called the hash table. The hash table is often an
array whose elements are called buckets (these are the buckets that
the ratio is talking about). A bucket will hold all of the key/value
pairs that hash to the same number:
#!/usr/bin/perl
use strict;
use warnings;
sub weak_hash {
my $key =3D shift;
my $hash =3D 1;
for my $character (split //, $key) {
$hash *=3D ord $character;
}
return $hash;
}
sub create {
my ($size) =3D [at] _;
my [at] hash_table;
#set the size of the array
$#hash_table =3D $size - 1;
return \ [at] hash_table;
}
sub store {
my ($hash_table, $key, $value) =3D [at] _;
#create an index into $hash_table
#constrain it to the size of the hash_table
my $hash_table_size =3D [at] $hash_table;
my $index =3D weak_hash($key) % $hash_table_size;
#push the key/value pair onto the bucket at the index
push [at] {$hash_table->[$index]}, {
key =3D> $key,
value =3D> $value
};
return $value;
}
sub retrieve {
my ($hash_table, $key) =3D [at] _;
#create an index into $hash_table
#constrain it to the size of the hash_table
my $hash_table_size =3D [at] $hash_table;
my $index =3D weak_hash($key) % $hash_table_size;
#get the bucket for this key/value pair
my $bucket =3D $hash_table->[$index];
#find the key/value pair in the bucket
for my $pair ( [at] $bucket) {
return $pair->{value} if $pair->{key} eq $key;
}
#if key isn't in the bucket:
return undef;
}
sub list_keys {
my ($hash_table) =3D [at] _;
my [at] keys;
for my $bucket ( [at] $hash_table) {
for my $pair ( [at] $bucket) {
push [at] keys, $pair->{key};
}
}
return [at] keys;
}
sub print_hash_table {
my ($hash_table) =3D [at] _;
for my $i (0 .. $#$hash_table) {
print "in bucket $i:\n";
for my $pair ( [at] {$hash_table->[$i]}) {
print "$pair->{key} =3D> $pair->{value}\n";
}
}
}
my $hash_table =3D create(3);
my $i =3D 0;
for my $key (qw/a b c d g j/) {
store($hash_table, $key, $i++);
}
print_hash_table($hash_table);
print "the a key holds: ", retrieve($hash_table, "a"), "\n";
As we can see from this example, it is possible for one bucket have
more key/value pairs than the others. This is a bad situation to be
in. It cause the hash to be slow that bucket. This is one of the
uses of the ratio of used to total buckets that hashes return in
scalar context. If the hash says that only a few buckets are being
used, but they are lots of keys in the hash, then you know you have a
problem.
To learn more about hashes, ask questions here about what I have said,
or [read about them][4].
[0]: http://en.wikipedia.org/wiki/Md5
[1]: http://en.wikipedia.org/wiki/Sha1
[2]: http://en.wikipedia.org/wiki/Jenkins_hash_function#one-at-a- time
[3]: http://en.wikipedia.org/wiki/Modulo_operation
[4]: http://en.wikipedia.org/wiki/Hash_table
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: assigning hash to a scalar
--000e0cd4d4dc60efe9049f64e14a
Content-Type: text/plain; charset=ISO-8859-1
Chas++
I don't get to see Algorithms in college, so I doubly appreciate
explanations like this, in the one language I sort of grok* :) You've made
my morning, thank you!
Brian.
*Also the reason I'm trying to hunt down a copy of Mastering Algorithms with
Perl.
On Sat, Mar 26, 2011 at 8:40 AM, Chas. Owens <chas.owens [at] gmail.com> wrote:
> On Sat, Mar 26, 2011 at 03:53, Sunita Rani Pradhan
> <Sunita.Pradhan [at] altair.com> wrote:
> > Thanks Alan . I had got this piece of info from google but I do not
> > understand clearly what it wants to define . It would be good , if you
> > can explain bit more .
> snip
>
> If you want to know how many key/value pairs are in a hash, you can say
>
> my $count = keys %hash;
>
> If you want a reference to %hash, you can say
>
> my $ref = \%hash;
>
> Saying
>
> my $hash_info = %hash;
>
> Will get you either 0 (if the hash is empty) or the ratio of used to
> total buckets. This information is almost, but not completely,
> useless to you. To understand what that means you must first
> understand how hashing works.
>
> Lets implement a hash using Perl 5. The first thing we need is a
> hashing function. Hashing functions turn strings into, hopefully,
> unique numbers. Examples of real strong hashing functions are
> [MD5][0] or [SHA1][1], but they tend to be too slow for common use, so
> people tend to use weaker (i.e. ones that produce less unique output)
> functions for hash tables. Perl 5 uses Bob Jenkins [one-at-a-time]
> algorithm, which has a nice tradeoff of uniqueness to speed. For our
> example, I will use a very weak hashing function:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> sub weak_hash {
> my $key = shift;
> my $hash = 1;
> #multiply every character in the string's ASCII/Unicode value
> together
> for my $character (split //, $key) {
> $hash *= ord $character;
> }
> return $hash;
> }
>
> for my $string (qw/cat dog hat/) {
> print "$string hashes to ", weak_hash($string), "\n";
> }
>
> Because hashing functions tend to give back number that are far apart,
> you usually use [modulo][3] to reduce the range of numbers it gives
> back:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> sub weak_hash {
> my $key = shift;
> my $hash = 1;
> #multiply every character in the string's ASCII/Unicode value
> together
> for my $character (split //, $key) {
> $hash *= ord $character;
> }
> return $hash;
> }
>
> for my $string (qw/cat dog hat/) {
> # the % operator is constraining the number
> # weak_hash returns to 0 - 10
> print "$string hashes to ", weak_hash($string) % 11, "\n";
> }
>
> Now that we have a hashing function, we need somewhere to save the key
> and value. This is called the hash table. The hash table is often an
> array whose elements are called buckets (these are the buckets that
> the ratio is talking about). A bucket will hold all of the key/value
> pairs that hash to the same number:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> sub weak_hash {
> my $key = shift;
> my $hash = 1;
> for my $character (split //, $key) {
> $hash *= ord $character;
> }
> return $hash;
> }
>
> sub create {
> my ($size) = [at] _;
>
> my [at] hash_table;
>
> #set the size of the array
> $#hash_table = $size - 1;
>
> return \ [at] hash_table;
> }
>
>
> sub store {
> my ($hash_table, $key, $value) = [at] _;
>
> #create an index into $hash_table
> #constrain it to the size of the hash_table
> my $hash_table_size = [at] $hash_table;
> my $index = weak_hash($key) % $hash_table_size;
>
> #push the key/value pair onto the bucket at the index
> push [at] {$hash_table->[$index]}, {
> key => $key,
> value => $value
> };
>
> return $value;
> }
>
> sub retrieve {
> my ($hash_table, $key) = [at] _;
>
> #create an index into $hash_table
> #constrain it to the size of the hash_table
> my $hash_table_size = [at] $hash_table;
> my $index = weak_hash($key) % $hash_table_size;
>
> #get the bucket for this key/value pair
> my $bucket = $hash_table->[$index];
>
> #find the key/value pair in the bucket
> for my $pair ( [at] $bucket) {
> return $pair->{value} if $pair->{key} eq $key;
> }
>
> #if key isn't in the bucket:
> return undef;
> }
>
> sub list_keys {
> my ($hash_table) = [at] _;
>
> my [at] keys;
>
> for my $bucket ( [at] $hash_table) {
> for my $pair ( [at] $bucket) {
> push [at] keys, $pair->{key};
> }
> }
>
> return [at] keys;
> }
>
> sub print_hash_table {
> my ($hash_table) = [at] _;
>
> for my $i (0 .. $#$hash_table) {
> print "in bucket $i:\n";
> for my $pair ( [at] {$hash_table->[$i]}) {
> print "$pair->{key} => $pair->{value}\n";
> }
> }
> }
>
> my $hash_table = create(3);
>
> my $i = 0;
> for my $key (qw/a b c d g j/) {
> store($hash_table, $key, $i++);
> }
> print_hash_table($hash_table);
>
> print "the a key holds: ", retrieve($hash_table, "a"), "\n";
>
> As we can see from this example, it is possible for one bucket have
> more key/value pairs than the others. This is a bad situation to be
> in. It cause the hash to be slow that bucket. This is one of the
> uses of the ratio of used to total buckets that hashes return in
> scalar context. If the hash says that only a few buckets are being
> used, but they are lots of keys in the hash, then you know you have a
> problem.
>
> To learn more about hashes, ask questions here about what I have said,
> or [read about them][4].
>
> [0]: http://en.wikipedia.org/wiki/Md5
> [1]: http://en.wikipedia.org/wiki/Sha1
> [2]: http://en.wikipedia.org/wiki/Jenkins_hash_function#one-at-a- time
> [3]: http://en.wikipedia.org/wiki/Modulo_operation
> [4]: http://en.wikipedia.org/wiki/Hash_table
>
> --
> Chas. Owens
> wonkden.net
> The most important skill a programmer can have is the ability to read.
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--000e0cd4d4dc60efe9049f64e14a--
Re: assigning hash to a scalar
Hi Chas, and Brian (and all),
On Saturday 26 Mar 2011 18:05:33 Brian Fraser wrote:
> Chas++
>
> I don't get to see Algorithms in college, so I doubly appreciate
> explanations like this, in the one language I sort of grok* :) You've made
> my morning, thank you!
Well, I've only skimmed Chas' response, but it seems to be very comrpehensive,
and researched. So I agree it's well done. It's too bad you (= Brian) don't
get to see such data structures in your college, because I think they are an
essential knowledge for every half-serious software developer. To quote ESR
(inspired by Fred Brooks from "The Mythical Man-Month") from "The Cathedral
and the Bazaar" (also a must read for most enlightened programmers, IMO):
Smart data structures and dumb code works a lot better than the
other way around.
Normally, it's more important to understand data structures and to know when
to know the tradeoffs of each ones, than it is to be familiar with all the
various algorithms in the literature.
I have studied about hashes during my Electrical Engineering (more like
Computer Engineering in the United States) degree, and we were shown many
variations on the theme of hash tables and there are more explanations about
them here:
* http://blog.reverberate.org/2009/03/01/the-art-of-hashing/
* http://tech.groups.yahoo.com/group/hackers-il/message/1831
>
> Brian.
>
> *Also the reason I'm trying to hunt down a copy of Mastering Algorithms
> with Perl.
>
Well, Mastering Algorithms with Perl does not cover hashing unfortunately, but
I remember it as an overall good book for when I read it (though far from
perfect). It's a bit showing its age, though. You may wish to consider this
book instead:
* http://www.algorist.com/
* http://www.amazon.com/Algorithm-Design-Manual-Steven-Skiena/ dp/1848000693
I've read it after I knew most of the material more formally, and was still
impressed by it, and it takes a more accessible and down-to-earth approach
than CLR/CLRS ( http://en.wikipedia.org/wiki/Introduction_to_Algorithms ) to
say nothing of Knuth's "The Art of Computer Programming", which appears to be
something that mere mortals cannot hope to understand. It primarily uses
examples in C, though (possibly for a very good reason), but feels much
fresher and more accurate than the "Mastering Algorithms with Perl" book.
There have been other offline and online books about data structures and
algorithms and you can find some in a web search. This seems OKish and it's
online, under Creative Commons by-sa, and also a wikibook:
* http://en.wikibooks.org/wiki/Data_Structures
Regards,
Shlomi Fish
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://shlom.in/enemy
I hope that you agree with me that 99.9218485921% of the users wouldn't bother
themselves with recompilation (or any other manual step for that matter) to
make their games run 1.27127529900685765% faster ;-) -- Nadav Har'El
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: assigning hash to a scalar
On Sat, Mar 26, 2011 at 12:05, Brian Fraser <fraserbn [at] gmail.com> wrote:
> Chas++
>
> I don't get to see Algorithms in college, so I doubly appreciate
> explanations like this, in the one language I sort of grok* :) You've made
> my morning, thank you!
snip
Be warned, my implementation is incredibly naive and inefficient.
This was the just the bare minimum needed to understand the meaning of
a hash's scalar value.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/