Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: count sort & output

by sauoq (Abbot)
on Dec 19, 2002 at 04:57 UTC ( [id://221048]=note: print w/replies, xml ) Need Help??


in reply to count sort & output

my @patterns = qw( a b c d e f g h i j k l m n o p q r s t u v w x y z + ); my %hash; my $data = do { local $/; <DATA> }; for (@patterns) { $hash{$_}++ for $data =~ /\Q$_\E/g; } my $counter = 0; for (sort {$hash{$b} <=> $hash{$a}} keys %hash) { print "$_ - $hash{$_}\n"; unless (++$counter % 10) { print "Press Enter"; <STDIN> } } __END__ Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal.
-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: count sort & output
by Anonymous Monk on Dec 19, 2002 at 11:37 UTC

    A less verbose way to do something similar:

    my $data = do { local $/; <DATA> }; my $patt = join '|' => map quotemeta, 'a' .. 'z'; my %freq; $freq{$1}++ while $data =~ /($patt)/go; # etc...

    Of course, counting occurrences of mere letters should normally be done using tr. ;-)

    Also, mkent may want to save intermediate (sorted) results to avoid duplicating work.

      Of course I agree that counting letters should be done with tr, so assuming that the strings could be longer and are not required to be of the same length, you're code has a potential subtle bug.

      If both "yahoo.com" and "yahoo.commies.org" are to be searched for, its chances of finding the second are dependent on its position in the regular expression relative to the first. You would have to sort your strings by length and build your regular expression with the longest ones first.

      My code has its own sort of bug(?) in that both "www.yahoo.com" and "yahoo.com" might be found in the string "www.yahoo.commies.org"... however, it works correctly given the poorly stated requirements.

      -sauoq
      "My two cents aren't worth a dime.";
      
        Your code has a potential subtle bug. You would have to sort your strings by length and build your regular expression with the longest ones first.

        You are of course right about the bug lurking in that snippet (sorting the strings in reverse order will fix this). My point was to let the regex engine do its work instead of the nested loop. That, and the suggestion to store intermediate results that would make for slowness.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-06-14 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.