Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Top five words by occurrence

by Tanktalus (Canon)
on Jul 18, 2005 at 16:46 UTC ( #475809=note: print w/replies, xml ) Need Help??

in reply to Top five words by occurrence

Two things - first, it'd be handy to have your sample input as well. Second, what you probably want to do is continue using split, but then just clear off all trailing punctuation:

foreach my $wd (@words) { $wd =~ s/[[:punct:]]+$//; next if length($wd) < 5; $count{$wd}++; }
This will remove trailing apostrophes, such as "wha' zup?", but not ones in the middle of words, such as "ain't".

Oh, I lied - three things. Use strict and warnings. Just do. I added a "my" to the foreach above for strictness purposes - do the same for your other foreach.

Once you have all this, you should be able to sort on value, foreach my $w (sort { $count{$a} <=> $count{$b} } keys %count) { ... } to get everything in order. What you do with it after that depends on how you want to deal with multiple words in 5th place.

PS: best use of split with no parameters I've ever seen. I don't think that's even close to your problem, but is, in fact, the best part of your code. Good job. :-)

Replies are listed 'Best First'.
Re^2: Top five words by occurrence
by ghettofinger (Monk) on Jul 18, 2005 at 16:51 UTC
    Here is the test I am going through:
    The Declaration of Independence of the Thirteen Colonies In CONGRESS, July 4, 1776 <readmore> The unanimous Declaration of the thirteen united States of America, When in the Course of human events, it becomes necessary for one peopl +e to dissolve the political bands which have connected them with anot +her, and to assum e among the powers of the earth, the separate and equal station to whi +ch the Laws of Nature and of Nature's God entitle them, a decent resp +ect to the opinio ns of mankind requires that they should declare the causes which impel + them to the separation. We hold these truths to be self-evident, that all men are created equa +l, that they are endowed by their Creator with certain unalienable Ri +ghts, that among these are Life, Liberty and the pursuit of Happiness. --That to secure + these rights, Governments are instituted among Men, deriving their j +ust powers from t he consent of the governed, --That whenever any Form of Government bec +omes destructive of these ends, it is the Right of the People to alte +r or to abolish i t, and to institute new Government, laying its foundation on such prin +ciples and organizing its powers in such form, as to them shall seem +most likely to ef fect their Safety and Happiness. Prudence, indeed, will dictate that G +overnments long established should not be changed for light and trans +ient causes; and accordingly all experience hath shewn, that mankind are more disposed +to suffer, while evils are sufferable, than to right themselves by ab +olishing the form s to which they are accustomed. But when a long train of abuses and us +urpations, pursuing invariably the same Object evinces a design to re +duce them under a bsolute Despotism, it is their right, it is their duty, to throw off s +uch Government, and to provide new Guards for their future security. +—Such has been th e patient sufferance of these Colonies; and such is now the necessity +which constrains them to alter their former Systems of Government. Th +e history of the present King of Great Britain [George III] is a history of repeated in +juries and usurpations, all having in direct object the establishment + of an absolute T yranny over these States. To prove this, let Facts be submitted to a c +andid world. He has refused his Assent to Laws, the most wholesome and necessary fo +r the public good. He has forbidden his Governors to pass Laws of immediate and pressing +importance, unless suspended in their operation till his Assent shoul +d be obtained; an d when so suspended, he has utterly neglected to attend to them. He has refused to pass other Laws for the accommodation of large distr +icts of people, unless those people would relinquish the right of Rep +resentation in th e Legislature, a right inestimable to them and formidable to tyrants o +nly. He has called together legislative bodies at places unusual, uncomfort +able, and distant from the depository of their public Records, for th +e sole purpose of fatiguing them into compliance with his measures. He has dissolved Representative Houses repeatedly, for opposing with m +anly firmness his invasions on the rights of the people. He has refused for a long time, after such dissolutions, to cause othe +rs to be elected; whereby the Legislative powers, incapable of Annihi +lation, have retu rned to the People at large for their exercise; the State remaining in + the mean time exposed to all the dangers of invasion from without, a +nd convulsions wi thin. He has endeavoured to prevent the population of these States; for that + purpose obstructing the Laws for Naturalization of Foreigners; refus +ing to pass other s to encourage their migrations hither, and raising the conditions of +new Appropriations of Lands. He has obstructed the Administration of Justice, by refusing his Assen +t to Laws for establishing Judiciary powers. He has made Judges dependent on his Will alone, for the tenure of thei +r offices, and the amount and payment of their salaries. He has erected a multitude of New Offices, and sent hither swarms of O +fficers to harass our p

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://475809]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-10-30 01:35 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (276 votes). Check out past polls.