Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > HASH

Reply
 
 
Winston
Guest
Posts: n/a
 
      12-06-2006
I need to *MAKE* some subrutines for search inside a hash.
I wonder what is the best for this.

For example, I have a hash with names and ages. And I need to make some
searches using regular expressions...

sub a_option {
%hash = @_;
print "Letter? ";
chomp($letter = <STDIN>);
@names = keys(%hash);
$found=0;
foreach $name (@names) {
if ($name =~ /^$letter/) {
print "Name: $name\t\t Age:$hash{$name}";
$encontrado++;
}
}
}

And the same for b_option, c_option, etc...
Is this ok? Or is a little bit ridiculous?

Is correct to do: %hash=@_ ?

These are my first steps in PERL. Is this the correct group?
Don't reply with a 1-line-perl-code that make all I need!
Thanks!
 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      12-06-2006
Winston <(E-Mail Removed)> wrote:

> I need to *MAKE* some subrutines for search inside a hash.
> I wonder what is the best for this.
>
> For example, I have a hash with names and ages. And I need to make some
> searches using regular expressions...
>
> sub a_option {
> %hash = @_;
> print "Letter? ";
> chomp($letter = <STDIN>);
> @names = keys(%hash);
> $found=0;
> foreach $name (@names) {
> if ($name =~ /^$letter/) {
> print "Name: $name\t\t Age:$hash{$name}";
> $encontrado++;
> }
> }
> }
>
> And the same for b_option, c_option, etc...
> Is this ok? Or is a little bit ridiculous?
>
> Is correct to do: %hash=@_ ?
>
> These are my first steps in PERL.


The language is called Perl, not PERL (Nor is it really an acronym).

> Is this the correct group?


Yes, if you take the time to read the posting guidelines.


> Don't reply with a 1-line-perl-code that make all I need!
> Thanks!


Ok, put at the very start of your program:

use strict;
use warnings;

Furthermore: name your subs right. I doubt if a_option is clear to you in
a week or two.

--
John Experienced Perl programmer: http://castleamber.com/

Perl help, tutorials, and examples: http://johnbokma.com/perl/
 
Reply With Quote
 
 
 
 
gf
Guest
Posts: n/a
 
      12-06-2006
Winston wrote:
> I need to *MAKE* some subrutines for search inside a hash.
> I wonder what is the best for this.
>
> For example, I have a hash with names and ages. And I need to make some
> searches using regular expressions...
>
> sub a_option {
> %hash = @_;
> print "Letter? ";
> chomp($letter = <STDIN>);
> @names = keys(%hash);
> $found=0;
> foreach $name (@names) {
> if ($name =~ /^$letter/) {
> print "Name: $name\t\t Age:$hash{$name}";
> $encontrado++;
> }
> }
> }
>
> And the same for b_option, c_option, etc...
> Is this ok? Or is a little bit ridiculous?


Not ridiculous, just going about it the hard way.

>
> Is correct to do: %hash=@_ ?


I wouldn't because it won't scale well... actually, it'll scale very
badly.

OK Grasshopper, grab your Camel book and prepare to do some lookin' up.
I won't write it for you, but I will point in the directions I'd go.

Pass the hash as a reference for speed...

....a_option(\%hash_to_scan);
....
sub a_option {

# make sure you got a hash-ref and it contained something. This is
called defensive programming and is good for speed and robustness.
my $href = shift or return;
my %hash = %{$href};
scalar(keys(%hash)) or return;
....

# You don't wanna go this way...
# @names = keys(%hash);
# $found=0;

# prebuild your regexp for speed. Speed is good.
my $pattern = qr/^$letter/;
# filter all the keys that match...
foreach my $name (grep {$pattern} keys %hash) {
....
}
>
> These are my first steps in PERL. Is this the correct group?
> Don't reply with a 1-line-perl-code that make all I need!
> Thanks!


 
Reply With Quote
 
Brian Helterline
Guest
Posts: n/a
 
      12-06-2006
gf wrote:
> Winston wrote:
>
>>Is correct to do: %hash=@_ ?

>
>
> I wouldn't because it won't scale well... actually, it'll scale very
> badly.
>
> OK Grasshopper, grab your Camel book and prepare to do some lookin' up.
> I won't write it for you, but I will point in the directions I'd go.
>
> Pass the hash as a reference for speed...


You want speed...

>
> ...a_option(\%hash_to_scan);
> ...
> sub a_option {
>
> # make sure you got a hash-ref and it contained something. This is
> called defensive programming and is good for speed and robustness.
> my $href = shift or return;
> my %hash = %{$href};


and then immediately dereference. Not much different than passing the hash.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-07-2006
Brian Helterline schreef:
> gf:


>> Pass the hash as a reference for speed...

>
> You want speed... [...]
>
>> my %hash = %{$href};

>
> and then immediately dereference. Not much different than
> passing the hash.


AFAIK, passing the hash itself, actually passes a flattened copy. So
there is a big difference.

$ perl -wle '%x=(A=>1,B=>2,C=>3);sub x{%_=@_;$_{'A'}=-9};x(%x);print %x'
A1C3B2

$ perl -wle '%x=(A=>1,B=>2,C=>3);sub x{$_[0]->{'A'}=-9};x(\%x);print %x'
A-9C3B2

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      12-07-2006

Quoth "Dr.Ruud" <(E-Mail Removed)>:
> Brian Helterline schreef:
> > gf:

>
> >> Pass the hash as a reference for speed...

> >
> > You want speed... [...]
> >
> >> my %hash = %{$href};

> >
> > and then immediately dereference. Not much different than
> > passing the hash.

>
> AFAIK, passing the hash itself, actually passes a flattened copy.


The point is that %$href also returns a flattened copy, so 'gf' was
copying the hash in any case.

Ben

--
We do not stop playing because we grow old;
we grow old because we stop playing.
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
gf
Guest
Posts: n/a
 
      12-07-2006

Brian Helterline wrote:

> and then immediately dereference. Not much different than passing the hash.


Unless the hash is really big, then you've got Perl shoving all the
contents of the hash into the sub as a humongeous array instead of a
single hash-ref value. Do that in a loop with a lot of data, and your
program slows down.

To keep the speed up sometimes I'll keep the hash-ref and just access
the keys using the -> operator, but I find it to be visually noisy, so
when speed isn't an issue I'll dereference into a local hash and go
from there. At least it's an informed decision, one I make again and
again... change, back out the change... benchmark... change again....

 
Reply With Quote
 
Darren Dunham
Guest
Posts: n/a
 
      12-08-2006
Brian Helterline <(E-Mail Removed)> wrote:
> gf wrote:
>> Pass the hash as a reference for speed...

> You want speed...


>> ...a_option(\%hash_to_scan);
>> ...
>> sub a_option {
>>
>> # make sure you got a hash-ref and it contained something. This is
>> called defensive programming and is good for speed and robustness.
>> my $href = shift or return;
>> my %hash = %{$href};


> and then immediately dereference. Not much different than passing the
> hash.


It seems completely different to me. The reference is a constant
penalty to dereference. Otherwise, it's a penalty to copy the data. So
as the contents of the hash increase in size, the cost to copy
increases.

If the hash is sizeable (megabytes), the you're saving time and memory.
If the hash will always be tiny, then you're probably not saving
anything.

--
Darren Dunham (E-Mail Removed)
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-08-2006
Ben Morrow schreef:
> Dr.Ruud:
>> Brian Helterline:
>>> gf:


>>>> Pass the hash as a reference for speed...
>>>
>>> You want speed... [...]
>>>
>>>> my %hash = %{$href};
>>>
>>> and then immediately dereference. Not much different than
>>> passing the hash.

>>
>> AFAIK, passing the hash itself, actually passes a flattened copy.

>
> The point is that %$href also returns a flattened copy, so 'gf' was
> copying the hash in any case.


Ah yes, I concentrated on the "dereferencing" and missed that gf built a
hash with it.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Is there a hash algorithm with direct access to hash elements andreference count? Bo Peng C++ 4 03-12-2006 05:57 AM
In 'HashMap.put', "if (e.hash == hash && eq(k, e.key))" ? Red Orchid Java 3 01-30-2006 07:04 PM
standard library for hash table storage and hash algorithm Pieter Claassen C Programming 1 08-04-2004 03:11 AM



Advertisments