Forum Moderators: coopster & phranque

Message Too Old, No Replies

Need help in sorting in this script

         

sumeet ruiwale

6:01 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



Hi,
i am having problems wile sorting the log file.. when it contains multiple year entries. The sort does not work properly if there are dates with two or more different years in the input file . this is code i have so far.. pls help me with it..

#! /usr/bin/perl

## Include the required modules
use File::Basename;
use File::Copy;
use Getopt::Long;
use Cwd;
use Time::localtime;

use Date::Calc qw(Week_Number);
#Package sort_sub
$debug = 0;

if ( GetOptions("-f=s" => \$file,
"-d=i" => \$debug, ) ) {

#print "hello\n";
}
else {
print "Error! Bad command line arguments\n";
print "work_week.pl -f <file_name> <-d 1>\n";
print "options: -d is debug mode\n";
die "Aborting script - fix command line arguments!\n";
}

#declare hash for counting work-weeks and storing year
# hash key is work-week
# hash value is count
%weeks;

# Open input files for reading
open( FILE, "< $file" )
or die "ERROR: Couldn't open $file for reading: $!\n";

# Read input file
while (<FILE>) {

chomp;
$LINE = $_;

# parse the date
# Assumes the format is: 05/09/2005 11:59 AM 720 perwin
($mm, $dd, $yyyy) = ($LINE =~ /^(\d+)\/(\d+)\/(\d+)/);

if ($debug==2) {
print "month = $mm, date = $dd, year = $yyyy\n";
}

# using Date:: Calc module
$wnum = Week_Number($yyyy, $mm, $dd);
$wnum = $wnum . " " . $yyyy;

if ($debug) {
print " workweek = $wnum using Date module\n";
}

if ( exists($weeks{$wnum}) ) {

# this week and year already encountered - increment count
$weeks{$wnum}++;
if ($debug) { print "$weeks{$wnum}\n"; }

}
else {
# this is the first instance of this ww and year - insert into hash
# set count to 1
$weeks{$wnum} = 1;
if ($debug) { print "set count to $weeks{$wnum} for Week: $wnum\n"; }
}
}

#Now sort and print
print "Week Numer\t\tNo. of Users\n";
print "======================================\n";
foreach $key ( sort { $a <=> $b } keys %weeks ) {
print "Work Week $key \t\t $weeks{$key}\n";
}

SeanW

11:02 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



[perl]
$wnum = $wnum . " " . $yyyy;
[/perl]

Try the other way around

[perl]
$wnum = $yyyy . " " . $wnum;
[/perl]

sumeet ruiwale

3:33 pm on Jul 7, 2005 (gmt 0)

10+ Year Member



does anyone know how to install modules.. its saying date.calc not found. whihc module do i have to install. and one more thing, how do i make this cript read from a file rather than a comman line fucntion as i have written.