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!

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.

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

Brian Helterline <> 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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!