CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Need feedback on the following prg.

 



w020637
Novice

Jan 30, 2008, 7:52 AM

Post #1 of 3 (472 views)
Need feedback on the following prg. Can't Post

#!/usr/bin/perl -w
# This Program validates the record length of a fixed length file
# for each record ,validates the delimiter count,checks the existence and
# verifies whether it a good file or not.
# Usage: This program expects the length of the fixed delimited file.
# Execution: perl validate.pl <fixed record length> <delimiter> <no. of delimiter in a rec>
# Delimiter Usage: use 1 for | and 2 for comma.
#####################################################
# Check if the necessary number of arguments have been
# given at command prompt

if ($#ARGV != 2 ) {
print "usage: Validate.pl <fixed record length> <delimiter> <no. of delimiter in a rec>\n";
exit;
}
$INPUT_FILE = "/home/jsingh/perl/a.txt";

#Chchecks the existence and verifies whether it a good file or not.

if (-e $INPUT_FILE){
if (-z $INPUT_FILE){
print("The file $INPUT_FILE is a zero byte file\n");
exit;
}
else{
print ("The file $INPUT_FILE is a good file\n");
}
}else{
print("The file $INPUT_FILE does not exist\n");
exit;
}
# Take the record length from the command line
my $del1;
$frl=$ARGV[0];
print("The fixed record length of the file is=$frl \n");
$del=$ARGV[1];
print("The delimiter of the file is : $del \n");
# Number of delimeter expected in each record.
$nop=$ARGV[2];
print("The number of delimiters in a record should be =$nop\n");
# The file for which the checks have to be performed.
open(INPUT_FILE);
@array = <INPUT_FILE>;
close(INPUT_FILE);
# Read each record into an array.
foreach (@array) {
$firstVar = $_;
chop($firstVar);
#Counts the number of delimiters in the record.
if ($del == 1) {
$count = ($firstVar =~ tr/|//);
$del1="|";
}else{
$count = ($firstVar =~ tr/\,//);
$del1=",";
}

# If the count of delimiter's is more then expected print that record.
if($count != $nop){
print "There should be $nop $del1 s and there are $count $del1 s in the string=$firstVar\n";
}
# Check the record length against the expected record length.
if (length($firstVar) != $frl) {
$length=length($firstVar);
print("$firstVar is out of sync with the file record length=$frl and is length=$length\n");
}
}


This program is running fine but I want to introduce some more chks whether the the file is in .UTF8 format or not

secondly i want this program to accept any delimiter apart from the | and ,...i am nt able to pass | from command line....

Thanks

w020637


KevinR
Veteran


Jan 30, 2008, 1:28 PM

Post #2 of 3 (468 views)
Re: [w020637] Need feedback on the following prg. [In reply to] Can't Post

overall:

the script looks beginner-ish. The logic is a bit tortured. That is not a criticism, just an observation.

Not using "strict" and "warnings" also indicates you are a beginner.

Use a hash table for the delimiter look-up:


Code
my %delimiters = ( 
1 => '|',
2 => ',',
3 => "\t",
);


ask questions if need be.
-------------------------------------------------


w020637
Novice

Jan 30, 2008, 2:42 PM

Post #3 of 3 (467 views)
Re: [KevinR] Need feedback on the following prg. [In reply to] Can't Post

Thanks Kevin..I will implement the recommended changes...

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives