Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

to sort hash wrt values corresponding to keys

by ansh batra (Friar)
on Nov 02, 2011 at 05:43 UTC ( #935302=perlquestion: print w/ replies, xml ) Need Help??
ansh batra has asked for the wisdom of the Perl Monks concerning the following question:

hi monks!!!
i want to write sorting algo to sort the hash keys but wrt to one of its value
hash={arr1{1,2,3,a},arr2{2,3,4,x},arr3{3,4,5,e},arr4{4,5,6,b}};

after sorting output should be say:
arr1,arr4,arr3,arr2
please dont consider the syntax, its just to explain the scenario
question : how can i make a hash with one key conating multiple values
any suggestions regarding my problem are welcome!!!

Comment on to sort hash wrt values corresponding to keys
Select or Download Code
Re: to sort hash wrt values corresponding to keys
by GrandFather (Cardinal) on Nov 02, 2011 at 06:07 UTC

    Your post title is "Comment on" so I will.

    First off "Comment on" is a bad title if you want to attract people to your question. It tells them nothing about the topic of your question and looks like a lazy newbie question so anyone with only a small amount of time is likely to skip over it. You have immediately filtered out some of the people most likely to be able to help you. Is that what you want? It sure doesn't help get good answers!

    The 'code' you posted is rubbish, or at least it is rubbish if intended to be Perl. We always consider the syntax. If you are too lazy to provide a decent code sample that we can work with you just filtered out another large percentage of the people who might otherwise have given you good answers.

    You say "output should be say", but you don't show us the code you are using to generate the output. How can we have any idea what you are actually trying to achieve? Your example data is obviously invented and doesn't tell us anything about your actual problem domain. Want to guess how many more possible replies you just lost?

    Your final question doesn't have anything to do with any of the preceding material. If you fixed the syntax for your sample 'code' then you have the answer right there! Ain't many people going to bother replying to that.

    So, I suggest:

    1. you use a good title for your question that will attract people with and interest and knowledge pertinent to your problem,
    2. you provide a good code sample that compiles and that we can run to reproduce the problem you have,
    3. you provide a description of why you want to solve the problem and how the code you have shown fails
    4. you provide a very small amount of input data if appropriate,
    5. you provide a sample of expected output data if appropriate,
    6. you provide a sample of actual output showing the error if appropriate,
    7. that you do the work to write a good node because that is actually the lazy option in the long run!
    True laziness is hard work
Re: to sort hash wrt values corresponding to keys
by mwp (Hermit) on Nov 02, 2011 at 06:11 UTC

    It sounds like you want a hash of lists. Check out perldsc. When you write your sort function you can tell it to use an element of the child structure.

    I hope I'm not doing your homework for you, but because I'm bored, beneath the spoiler is an attempt at the solution to the problem as I understand it. HTH.

    mwp
      thanks mwp!!!
      sorry GRANDFATHER ill take care of these issues from now on!!!
      m a little lazy and a newbie too

        Laziness is good. It's a Perl virtue. But only if you think in terms of the long haul. Doing spending the time to do something right the first time is way more lazy than doing a quick and dirty job then spending endless time trying to sort the problems out.

        I spent a lot of time not answering your question, and am spending even more time now, in the hope that you will pay more attention to being actually lazy rather than superficially lazy in the future. Because at the end of the day (or month, year, ... whatever) it should take less effort for me to get your superficial laziness problem sorted out now than to endlessly ask for clarification later.

        Of course if you can't be bothered learning to be really and truly lazy I'll just give up trying to help because that's the laziest option for me. ;)

        True laziness is hard work
      my %input_hash = (key1,2,key2,3,key3,1); code:

      foreach (sort {$input_hash{$a} <=> $input_hash{$b}} keys %input_hash){ print "$input_hash{$_} \n"; }

      if at all thats what you wanted.
        thanks ramprasad!!
        this would be enough for me

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2014-07-24 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (157 votes), past polls