Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Entering the land of Perl

by jwkrahn (Monsignor)
on Apr 04, 2013 at 23:30 UTC ( #1027047=note: print w/ replies, xml ) Need Help??


in reply to Entering the land of Perl

Please, critique this.

You say you want to "count letters in a string" but you are counting all characters.

$letters{$1}++ while $seq =~ /([[:alpha:]])/g;


You shouldn't use prototypes.    And besides you are just copying the contents of the hash anyway which you could do more simply as:

sub sort_and_print_hash_keys { my %hash = @_;


Comment on Re: Entering the land of Perl
Select or Download Code
Replies are listed 'Best First'.
Re^2: Entering the land of Perl
by manbroski (Initiate) on Apr 04, 2013 at 23:52 UTC

    Good call on the letters versus characters. But as far as the prototype passing, I was certain that it enforces a pass by reference. Notice that I have to dereference it after the passage. So the hope is that there is no copying.

      Hi manbroski,

      "..Notice that I have to dereference it after the passage.." Why is that?

      Did you also notice your subroutine sort_and_print_hash_keys definition

      sub sort_and_print_hash_keys (\%) {..
      and how you eventually used the it like so:
      sort_and_print_hash_keys(%letters); # you passed a hash variable not a + hash ref.
      ".. But as far as the prototype passing, I was certain that it enforces a pass by reference..."

      If I may suggest, you will do well yielding the wisdom of jwkrahn, as regard the usage of prototype for this reason:

      When you use a reference prototype, like "\$", "\@", "\%" "...those symbols don't actually say that you must pass in a scalar reference, an array reference, and a hash reference. Rather, they say you must pass in a scalar variable, an array variable, and a hash variable. That means that the compiler insists upon seeing a properly notated variable of the given type, complete with "$", "@", or "%" in that slot. You must not use a backslash. The compiler silently supplies the backslash for you... "
      from Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen
      -- by liverpole, under subheading Problems with Reference Prototypes

      Hope this helps.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me
      Notice that I have to dereference it after the passage. So the hope is that there is no copying.

      In your code you have:

      sub sort_and_print_hash_keys (\%) { my %hash = %{shift()}; foreach (sort keys %hash) { print "$hash{$_} "; } print "\n"; }

      Which is copying the entire hash.    If you didn't want to copy the hash you could do it like this:

      sub sort_and_print_hash_keys (\%) { my $hash = shift; foreach (sort keys %$hash) { print "$hash->{$_} "; } print "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027047]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2015-07-28 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls