Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Probelm with priority printing (hash or by an array)

by Anonymous Monk
on Nov 06, 2008 at 14:05 UTC ( #721990=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Though this is a simple problem, I am literally confused to proceed this further. I have got a list of names with their priority something like this:
1 Flu 1 Cold 2 Diabetics 3 Typhoid 4 Measle
It has more than 15 such names. And is not from any file or any thing. I can initialize them in the code. And the numbers are the priority for each of the names. The priority may be same for different names. What I actually have to do is to query from the database for these names by their Identifiers(not the priority number) and write the result to a file.

And when writing the output the result should be in the priority order. I am really confused, If using a hash I cant have the integers as a 'key' since there may be repetitive of priority number as in the example.
Where as I am not sure of the way to initialize the array with both the priority number and the name associated to each other. I have a working code that fetches from the database for these name with the priority.( have used hash here with 'key' as name and 'value' as priority) But not in the given order mentioned above.

Also, as the SQL query fetches from the database in anyorder its printing in the order that it fetches from the database with the priority number as 'qouted in the hash'.

Any suggestions on this guys? Thank for your time and suggestions.

Replies are listed 'Best First'.
Re: Probelm with priority printing (hash or by an array)
by shmem (Chancellor) on Nov 06, 2008 at 14:32 UTC

    You can sort the keys of a hash by value also. You can even sort them by value and by key:

    my %h; while (<DATA>) { chomp; my ($v, $k) = split; $h{$k} = $v; } print "$h{$_} $_\n" for sort { $h{$a} <=> $h{$b} || $a cmp $b } keys % +h; __DATA__ 1 Flu 1 Cold 2 Diabetics 4 Impatience 3 Laziness 2 Hubris 3 Typhoid 4 Measle 1 Procrastination

    Output:

    1 Cold 1 Flu 1 Procrastination 2 Diabetics 2 Hubris 3 Laziness 3 Typhoid 4 Impatience 4 Measle
Re: Probelm with priority printing (hash or by an array)
by eighty-one (Curate) on Nov 06, 2008 at 14:17 UTC

    Hello,

    The Perl hast How-To may be of use. A hash of arrays would solve the problem of having a hash of priorities with multiple diseases each having the same priority. But, an array of arrays would as well.

    What's in the database? Knowing how that data is presented might be helpful.

    EDIT:

    I think I might have a better idea of what you're looking for. You could try something like this:

    #!/usr/bin/perl -w use strict; my @diseases = (); $diseases[0] = ['flu','cold']; $diseases[1] = ['Diabetics']; $diseases[2] = ['Typhoid']; $diseases[3] = ['Measels']; for(my $priority = 0;$priority < scalar(@diseases);$priority++){ print "Priority $priority diseases:\n"; foreach(@{$diseases[$priority]}){ print "\t$_\n"; } }
    which produces output like this:
    Priority 0 diseases: flu cold Priority 1 diseases: Diabetics Priority 2 diseases: Typhoid Priority 3 diseases: Measels
    but within that inner foreach loop, run a query; something like "SELECT * WHERE disease_name = $_" and format and print the results as needed.

    EDIT #2: Just noticed; you need to add 1 to $priority when printing to make it display properly, since your priority list starts with '1' and not '0'.

      Hi, Thanks for your suggestion. But when printing the results from the database it prints in the same order as it fetches. Also, I have two sets of diseases. Say: disease_1:
      1 flu 1 cold 2 measles

      and another set of disease like:
      1 diabetes 2 typhoid

      Those two sets are distinguished based on the process they go for. there may be some disease which appears in both the sets.
      The diseases are not the exact names but an example of it. The Query fetches different process that the disease has passed through. But I think that doesn't matter us much(!) here. The problem is I am having the same problem again that is ,the order of the disease is not in the order that I have got in the array but IT comes randomly as it fetches in the database. for example:
      XXX:measles 45 35 67 1000 XXX:flu 67 35 45 7000 XXX:typhoid 56 78 89 1000

      Note the XXX:disease is the way the disease is stored inthe database.
      What I need to have is the exact order and priority number along with the exact names that I have stored in the array.
      Thanks for your suggestions.
Re: Probelm with priority printing (hash or by an array)
by pjotrik (Friar) on Nov 06, 2008 at 14:59 UTC
    (Originally posted to one of the dupe topics)

    The sane approach would be to store the priority in the database with the rest of the information and obtain the list sorted by the priority.

    If you can't do that, save the priorities in a hash, but reverse to what you show in your post:

    %priority = ( Flu => 1, Cold => 1, ...
    then, given you have your data from db in an AoA, sort it using something like @sorted = sort({$priority{$$a[0]} <=> $priority{$$b[0]}} @list); (I expect the disease names in the first field here)
      I was doing that way before posting here and sorted that with the value: something like this:
      foreach my $pri(sort {$prior{$a} <=> $prior{$b}} keys %prior) print $pri, $prior{$pri}, "\n";


      where 'prior' is the hash and I had them getting sorted if I print them right after sorting. But after getting the data from the database I would like to get them in the same order as I have initialized them in the hash/array. My problem is to get them sorted in the net report I am going to print by the priority. Also, I have two different sets of disease with the same disease repeating in both the sets.

      As in your suggestion, you mean @sorted as a new array which sorts by priority of the array obtained from the database? Could any one make me clear whether I have understand the point clearly?
      once again,Thanks for your suggestions
        Update: Sorry I tried through your suggestion and it gives an error message with
        Can't use string ("XXXX2: Multiple sclerosis") as an ARRAY ref while " +strict refs" in use at dna_reportertest line 139
        I cant really understand why is that so.. when it goes through all the other disease (nearly 5 before)? Any comments? Thanks
Re: Probelm with priority printing (hash or by an array)
by jethro (Monsignor) on Nov 06, 2008 at 15:12 UTC

    Simply strech the priority numbers so that no collisions exist anymore, so Flu=> 1, Cold => 2, Diabetes =>3.... If nobody told you in which order the Flu and Cold records should be printed, they might as well be printed ordered. Then you can simply store the names in an array and loop through the array

    If the method above is not possible: Store the names in an array, but because of the collisions either use an Array of Arrays, or, if you are uncomfortable with that, concatenate names with equal priority : "Flu,Cold". The concatenated values can be extracted again with split

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2021-12-08 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?