1758356 Members
2337 Online
108868 Solutions
New Discussion юеВ

Help: perl count/sort

 
SOLVED
Go to solution
Junior C.
Frequent Advisor

Help: perl count/sort

I have the following in a text file

05-JUL-06
05-JUL-06
05-JUL-06
05-JUL-06
05-JUL-06
08-JUL-06
08-JUL-06
08-JUL-06
10-JUL-06
15-JUL-06
15-JUL-06
15-JUL-06
15-JUL-06
25-JUL-06
25-JUL-06
01-AUG-06
01-AUG-06
02-AUG-06
02-AUG-06
05-AUG-06
05-AUG-06
07-AUG-06
07-AUG-06
07-AUG-06
07-AUG-06
09-AUG-06
09-AUG-06
09-AUG-06
09-AUG-06
11-AUG-06
11-AUG-06
14-AUG-06
14-AUG-06
14-AUG-06
14-AUG-06
14-AUG-06
14-AUG-06
14-AUG-06
14-AUG-06
18-AUG-06
18-AUG-06
18-AUG-06

I will like to produce the following output using perl.

05-JUL-06 : 5
08-JUL-06 : 3
10-JUL-06 : 1
15-JUL-06 : 4
25-JUL-06 : 2
01-AUG-06 : 2
02-AUG-06 : 2
05-AUG-06 : 2
07-AUG-06 : 4
09-AUG-06 : 4
11-AUG-06 : 2
14-AUG-06 : 8
18-AUG-06 : 3

Total : 42


I appreciate all help.

Thanks, in advance.


8 REPLIES 8
Alexander Chuzhoy
Honored Contributor

Re: Help: perl count/sort

The only problem with the following code is that it doesn't sort the date. You'll have to insert a subroutine on the line: "my @dates=sort keys %hash;" between sort and keys %hash. Let us know if you need more help.

use strict;
open FILE, "path to a file" or die "Couldn't open a file";
my $lines;
my %hash;
while () {
chomp;
$lines++;
$hash{$_}++;
}
my @dates=sort keys %hash;
for (@dates)
{
print $_." : ".$hash{$_}."\n";
}
print "Total: $lines.\n";
g33k
Valued Contributor

Re: Help: perl count/sort

if file is always sorted it's quite easy.

open (INPUT, @ARGV[0]);
@lines=;
close (INPUT);

#now you have readed all form file in lines array
$prev_line=@lines[0];
$act_count=1;
$total_count=0;
foreach $line (@lines){
total_count++;
if ($line eq $prev_line){
$act_count++;
}
else {
print "$prev_line : $act_count\n";
$perv_line = $line;
$act_count=1;
}
}

print "Total : $total_count\n";


it's all and should be OK if there are no blank lines(if there are you should tak care about them).
g33k
Valued Contributor

Re: Help: perl count/sort

anyway best solution is to learne perl yourself here is something for begginers... http://www.comp.leeds.ac.uk/Perl/start.html

for more help google is good (he always helped me). But if something is hard to understand and perl there are some hard construction, than ask us.
Junior C.
Frequent Advisor

Re: Help: perl count/sort

AC/g33k,

Thanks for your reply.

I'm new to perl. following are my script.

my $Process_File="test_file.txt";

open(FP,"$Process_File") or die "Could not open $Process_File: $!\n";

while () {
@words = split ' ';
foreach $word (@words) {
$count{$word}{$ARGV}++;
$count++;
}
}

foreach $word (keys %count) {
$down_date = "$word";
$tot_count = join(", ",
map "$_: $count{$word}{$_}",
sort keys %{$count{$word}});
print "$down_date $tot_count\n"
}

close (FP);
print " \n";
print "Total : $count\n";

output:

11-AUG-06 : 2
15-AUG-06 : 4
07-AUG-06 : 4
01-AUG-06 : 2
25-JUL-06 : 2
10-JUL-06 : 1
08-JUL-06 : 3
14-AUG-06 : 8
05-AUG-06 : 2
02-AUG-06 : 2
18-AUG-06 : 3
09-AUG-06 : 4
05-JUL-06 : 5

Total : 42



Please advise what I'm doing wrong. I need output sorted by date all JUL the AUG. AUG then SEP etc......

Thanks.

JC.
Alexander Chuzhoy
Honored Contributor

Re: Help: perl count/sort

The thing with hash is that its keys are not sorted. If you could put all entries in array instead (assuming that the file you read the info from is sorted), then you could work and assign hash keys/values on array items and also print array items respectively.
Hope it helps.
Alexander Chuzhoy
Honored Contributor
Solution

Re: Help: perl count/sort

To complete what I mean - I've changed my previous code a bit. Now I collect the data into array and then I do all manipulations.
This assures that the order in which the data was placed in the array remains intact.

use strict;
open FILE, "path_to_file" or die "Couldn't open a file";
my (@entries,@uniq,%hash,$lines);
while () {
chomp;
$lines++;
push @entries,$_; #place each in the array
}
for (@entries) {
push @uniq,$_ unless (defined($hash{$_})); #Need to avoid duplicated entries in output.
$hash{$_}++; #increment the count of duplicated lines.
}
print $_." : ".$hash{$_}."\n" for @uniq;
print "Total: $lines.\n";

Hope it helps.
Junior C.
Frequent Advisor

Re: Help: perl count/sort

Alexander,

Perfect. Thanks, I appreciate all your reply
Junior C.
Frequent Advisor

Re: Help: perl count/sort

Thread Close