HASH

W

Winston

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!
 
J

John Bokma

Winston said:
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.
 
G

gf

Winston said:
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) {
....
}
 
B

Brian Helterline

gf said:
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.
 
D

Dr.Ruud

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
 
B

Ben Morrow

Quoth "Dr.Ruud said:
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
 
G

gf

Brian said:
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....
 
D

Darren Dunham

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.
 
D

Dr.Ruud

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top