On 11-07-15 11:58 AM, Matt wrote:
The contents of the file are sorted already. Any ideas how to do this? Thanks.
Yes, see attached. -- Just my 0.00000002 million dollars worth, Shawn Confusion is the first step of understanding. Programming is as much about organization and communication as it is about coding. The secret to great software: Fail early & often. Eliminate software piracy: use only FLOSS.
#!/usr/bin/env perl # -------------------------------------- # name : freq # description : Count the items. # invocation : freq [--help] [--[no]reverse] [--[no]ignorecase] [--(count|alphabetically)] [<file>] ... # options : --help -- Print the help text. # --[no]reverse -- Reverse the order of the sort. # --[no]ignorecase -- Ignore case when sorting. # --(count|alphabetically) -- Sort by count or alphabetically. # arguments : [<file>] -- list of files to process. # environment : FREQ -- User assigned default options # return status : Zero is success. # i/o : NA # implementation : NA # examples : NA # see also : NA # -------------------------------------- # -------------------------------------- # Pragmas use strict; use warnings; # -------------------------------------- # Modules use Data::Dumper; $Data::Dumper::Sortkeys=1; use Getopt::Long; # -------------------------------------- # Configuration our $VERSION = '3.01'; # -------------------------------------- # Public # -------------------------------------- # Private my $ByCount = 0; my $Ignorecase = 0; my $Reverse = 0; my %Items = (); # -------------------------------------- # Main { init(); count(); show(); term(); } # -------------------------------------- # Subroutines # -------------------------------------- # subroutine : init # description : Set constants that can only be set a startup. # invocation : init(); # parameters : NA # returns : NA # i/o : NA # effects : $Reverse -- reverse order of sort. # $Ignorecase -- ignore case when sorting. # $ByCount -- sort by count, not alphabetically. # implementation : NA # examples : NA # -------------------------------------- sub init { my @freq = split /\s+/, $ENV{FREQ} || ''; unshift @ARGV, @freq; unless( GetOptions( 'help' => \&help, 'reverse!' => \$Reverse, 'ignorecase!' => \$Ignorecase, 'count' => sub{ $ByCount = 1 }, 'alphabetically' => sub{ $ByCount = 0 }, )){ system( "pod2usage $0" ); exit 1; } } # -------------------------------------- # subroutine : term # description : Cleanup the script. # invocation : term(); # parameters : NA # returns : NA # i/o : NA # effects : NA # implementation : NA # examples : NA # -------------------------------------- sub term { } # -------------------------------------- # subroutine : help # description : Print the help text. # invocation : help(); # parameters : NA # returns : NA # i/o : NA # effects : NA # implementation : Terminates script. # examples : NA # -------------------------------------- sub help { system( "pod2text $0" ); term(); exit 1; } # -------------------------------------- # subroutine : count # description : Count the items. # invocation : count(); # parameters : NA # returns : NA # i/o : NA # effects : %Items -- item => count # implementation : NA # examples : NA # -------------------------------------- sub count { while( <> ){ chomp; $Items{ $_ } ++; } } # -------------------------------------- # subroutine : show # description : Show the items and count. # invocation : show(); # parameters : NA # returns : NA # i/o : NA # effects : NA # implementation : NA # examples : NA # -------------------------------------- sub show { my @sorted = (); if( $ByCount ){ if( $Ignorecase ){ if( $Reverse ){ @sorted = map{ $_->[0] } sort{ $b->[1] <=> $a->[1] || $b->[2] cmp $a->[2] || $b->[0] cmp $a->[0] } map{[ $_, $Items{ $_ }, lc( $_ )]} keys %Items; }else{ @sorted = map{ $_->[0] } sort{ $a->[1] <=> $b->[1] || $a->[2] cmp $b->[2] || $a->[0] cmp $b->[0] } map{[ $_, $Items{ $_ }, lc( $_ )]} keys %Items; } }else{ if( $Reverse ){ @sorted = map{ $_->[0] } sort{ $b->[1] <=> $a->[1] || $b->[0] cmp $a->[0] } map{[ $_, $Items{ $_ }]} keys %Items; }else{ @sorted = map{ $_->[0] } sort{ $a->[1] <=> $b->[1] || $a->[0] cmp $b->[0] } map{[ $_, $Items{ $_ }]} keys %Items; } } }else{ if( $Ignorecase ){ if( $Reverse ){ @sorted = map{ $_->[0] } sort{ $b->[1] cmp $a->[1] || $b->[0] cmp $a->[0] } map{[ $_, lc( $_ )]} keys %Items; }else{ @sorted = map{ $_->[0] } sort{ $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] } map{[ $_, lc( $_ )]} keys %Items; } }else{ if( $Reverse ){ @sorted = sort{ $b cmp $a } keys %Items; }else{ @sorted = sort keys %Items; } } } binmode STDOUT, 'encoding(utf8)'; for my $item ( @sorted ){ my $count = $Items{ $item }; ( my $x = $item ) =~ s/([^\x20-\x7e])/sprintf"\\x{%02x}",ord($1)/ge; printf "%5d \xAB%s\xBB\n", $count, $x; } } __END__ =head1 NAME B<freq> - Count the items. =head1 SYNOPSIS freq [--help] [--[no]reverse] [--[no]ignorecase] [--(count|alphabetically)] [<file>] ... =head2 OPTIONS =over 4 =item --help Print out this text. =item --[no]reverse Reverse the sort order. Default is no reverse. =item --[no]ignorecase Ignore case when sorting. Default is to sort uppercase before lowercase. =item --(count|alphabetically) Sort output by count of items or alphabetically by item. Default is alphabetically. =back =head1 DESCRIPTION =head1 ENVIRONMENT =over 4 =item FREQ This variable allows the user to change his/her default options. =back =head1 FILES =head1 RETURN VALUE =head1 ERRORS =head1 EXAMPLES =head1 SEE ALSO =head1 STANDARDS =head1 BUGS =head1 HISTORY =over 4 =item On 2004-01-24 Created version 3.01. =back =head1 NOTES =cut
-- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/