Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A hash has only a single key and a single value, and each key must be unique. In your original problem you have this:</p> <pre><code>%hash = ( bob =&gt; "4.9", gita =&gt;"3.9 , 6,8", diu =&gt; "3.0", ); </code></pre> <p>Well, <code>gita</code> can't have two values. Nor, can you have two keys in your hash equal to <code>gita</code>. Thus, you can't use a simple hash to store your values.</p> <p>There are ways around this though by using references. For example, each element in your hash can contain a <a href="http://perldoc.perl.org/perlreftut.html" rel="nofollow">reference to an array</a>. Thus, your data structure can look like this:</p> <pre><code>%hash = ( bob =&gt; [(4.9)], gita =&gt; [(3.9, 6.8)], diu =&gt; [(3.0)], ); </code></pre> <p>The <code>[</code> and <code>]</code> marks a reference to an array. </p> <p>However, this wouldn't really solve your particular problem since you now have to go through each key in the hash, then each element in the array for each key, and sort those. You could create a sorting subroutine, but just because you can say <code>sort</code> doesn't make it efficient.</p> <p>Maybe what you need is an array of arrays. This will get rid of the issue you have with <code>gita</code> having two values, but make sorting a bit easier. Imagine a structure like this:</p> <pre><code>my @array = ( [bob =&gt; 4.9], [gita =&gt; 3.9], [gita =&gt; 6.8], [diu =&gt; 3.0], ); </code></pre> <p>Now, we can do a sort on <code>@array</code> depending upon the value of <code>$array[$x]-&gt;[1]</code>! All we need is for each element of the array <code>@array</code> is to compare $a->[1] with $b->[1]. Then, if we do a reverse sort on it, the biggest element will be <code>$array[0]</code>. The name is <code>$array[0]-&gt;[0]</code> and the element is <code>$array-&gt;[0]-&gt;[1]</code>.</p> <pre><code>#! /usr/bin/env perl use strict; use warnings; use feature qw(say switch); my @array = ( [bob =&gt; 4.9], [gita =&gt; 3.9], [gita =&gt; 6.8], [diu =&gt; 3.0], ); @array = reverse sort mysort @array; say "$array[0]-&gt;[0] $array[0]-&gt;[1]"; sub mysort { $a-&gt;[1] &lt;=&gt; $b-&gt;[1]; } </code></pre> <p>And the output is:</p> <pre><code>gita 6.8. </code></pre> <p>You notice that link to <a href="http://perldoc.perl.org/perlreftut.html" rel="nofollow">Perldoc's perllol</a>? I suggest you read it if you've never worked with Perl references before.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload