Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Hash sort again!!

by Anonymous Monk
on Apr 25, 2001 at 20:06 UTC ( [id://75511]=perlquestion: print w/replies, xml ) Need Help??

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

I realise I am stupid and shouldn't have been let loose on PERL in a million years but I need help!! Can anyone PLEASE help me with a hash sorting problem I am having?? I will be eternally grateful!!!!!!! I have successfully created a hash containing all the keys and values I want but wish to merely sort the keys into ascending numerical order. Am I doing something wrong with my hash assignment? I cannot quite see why it won't work See excerpt below
foreach $sentence(@sentences) { <BR> $total_sentence +=1; <BR> @words = split(/[^\w'a-zA-Z0-9_'-?]+/,$sentence); <BR> $Counter =0; <BR> foreach $word(@words){ <BR> $Counter+=1; <BR> } <BR> $sentence_count{($Counter)}=$sentence_count{($Counter)}+1;<BR> + } <BR> sort {$a<=>$b} (keys (%sentence_count)); <BR> print ("\nThere are a total of $total_sentence sentences in this text\ +n"); <BR> while (($sentence_count,$word_count) = each(%sentence_count)) { <BR> print ("\nThere are $sentence_count sentences of $word_count words +\n"); <BR> } <BR>

Replies are listed 'Best First'.
Help yourself - more.
by frankus (Priest) on Apr 25, 2001 at 20:17 UTC

    Calm centred thoughts, control your breathing, use English and HTML well to gain enlightenment.
    Hints:

    • Structure your question. Be concise and include all the facts.
    • Format your code without HTML in it. Let the <code> tag do all the work
    • Indicate what kind of sort you require, you're using a numeric sort but the code implies words.
    • Read Learning Perl, I do, when I fear I'm making a fundamental mistake
    • Join us and learn the ways of Perl.
    You'll find a great deal of satisfaction in being self-sufficient, and perhaps till then Perl will be a stranger to you.

    --
    
    Brother Frankus.
Re: Hash sort again!!
by suaveant (Parson) on Apr 25, 2001 at 20:31 UTC
    well.. your major problem is probably that you are just using sort by itself, as though it will make your hash sorted, it won't. sort returns you a list of your sorted keys so you want
    foreach (sort keys %sentance_count)
    try this...
    $total_sentence = @sentances; foreach $sentence (@sentences) { $count = scalar (split(/[^\w'a-zA-Z0-9_'-?]+/,$sentence)); $sentence_count{$count}++; } print ("\nThere are a total of $total_sentence sentences in this text\ +n"); foreach(keys %sentence_count) { print ("\nThere are $_ sentences of $sentence_count{$_} words\n"); }

                    - Ant
Re: Hash sort again!!
by rchiav (Deacon) on Apr 25, 2001 at 20:21 UTC
    First question is - what do you think you're sorting? The hash itself? If so, you can't sort a hash. You can however sort a list of the keys or values. You might want to look at how sort works. It will "return" a sorted list. That means that you have to assign the sort to an array. Then that array will contain a sorted list of the keys.

    Hope this helps..
    Rich

Re: Hash sort again!!
by coolmichael (Deacon) on Apr 26, 2001 at 12:52 UTC
    In addition to all the other suggestions, you could also try changing
    sort {$a<=>$b} (keys (%sentence_count));
    to
    my @sortedkeys = sort keys %sentence_count;

    sort is smart enough to figure out how to do the comparison for you. Once the array returned by keys is sorted, you need to assign it to another array. Sort won't change the order of the keys directly, but will return a new sorted array.

    michael
    the blue haired monk

Re: Hash sort again!!
by BMaximus (Chaplain) on Apr 25, 2001 at 20:25 UTC
    Perhaps you should take all those <BR>'s out of your code. Maybe then it will work. :)

    BMaximus

    Stupid is as Stupid does - Forest Gump
Re: Hash sort again!!
by arturo (Vicar) on Apr 26, 2001 at 01:37 UTC

    A couple of side notes that will simplify your code somewhat: once you've got the array of words, you don't need a foreach loop with the $Counter</code> increment to get the # of words in the array; a simple my $count = @words will do. Another little note: save some keystrokes,

    $sentence_count{$count}++;
    will increment $sentence_count{$count}.

    HTH

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-18 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found