Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

robby_dobby already pointed out the actual mistake: you need to pass fnd_max() a reference to the hash, since that's what fnd_max() is expecting. See how I pass my hashes to set_column widths().

Now beyond that: First, I'd use a more descriptive subroutine name, like "max_value_of_hash", and drop the prototype. Prototypes are advanced juju and shouldn't be used most of the time. Second, if you want to get the largest value from a hash, you don't need to access the keys at all. Here are some examples, starting with the simplest and wordiest:

#!/usr/bin/env perl use 5.010; use strict; use warnings; # newbie but clean version sub max_value_of_hash { my $h = shift; my $max = 0; for my $v (values %$h){ if ($v > $max){ $max = $v; # keep setting $max to larger value } } return $max; } # more perlish and elegant version sub max_value_of_hash2 { my $h = shift; my $max = 0; $_ > $max ? $max = $_ : undef for values %$h; return $max; } # let a module do it sub max_value_of_hash3 { use List::Util qw(max); return max values %{$_[0]}; } my %hash = ( a => 1, b => 2, c => 5, d => 3 ); say max_value_of_hash( \%hash); say max_value_of_hash2(\%hash); say max_value_of_hash3(\%hash);

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.


In reply to Re^11: Computing results through Arrays by aaron_baugher
in thread Computing results through Arrays by yasser8@gmail.com

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-18 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found