Re: Syntax Checker Script
- From: krahnj@xxxxxxxxx (John W . Krahn)
- Date: Tue, 30 Oct 2007 10:56:17 -0800
On Tuesday 30 October 2007 01:56, nishant.lokre@xxxxxxxxx wrote:
Hello,
Hello,
I want a script which is going to check if the numbers specified in
the config file is the same for each of the types of lines in the
seeding files.
The script would read each line to check if the no of columns is
correct as proposed in a config file.
If it is not correct, out put an entire list of lines which were not
correct with line number in OUTPUT file and exit with an error.
If all the lines were correct, exit normally.
Config.ini
ROUTER,22
IF,19
QOSINTERFACE,18
Seed.csv file
QOSINTERFACE,BusPhonePrefix,BusAreaCode,BusPhoneLast4
QOSINTERFACE,BusPhonePrefix,BusAreaCode,BusPhoneLast4
IF,BusPhonePrefix,BusAreaCode,BusPhoneLast4,BusAreaCode
IF,BusPhonePrefix,BusAreaCode,BusPhoneLast4
QOSINTERFACE,BusPhonePrefix,BusAreaCode,BusPhoneLast4
ROUTER,BusPhonePrefix,BusAreaCode,BusPhoneLast4,BusAreaCode,
QOSINTERFACE,BusPhonePrefix,BusAreaCode,BusPhoneLast4,BusAreaCode
ROUTER,BusAreaCode,BusPhonePrefix,BusPhoneLast4,BusPhonePrefix,
Below is the code for this particular requirement but right now
inputs are hard coded
I want that this script read from Config.ini file.
Can anybody help me out in this?
Yes, you should probably store the data from the file in a hash:
my $config_file = 'Config.ini';
open my $ch, '<', $config_file or die "Cannot open '$config_file' $!";
my %config;
while ( <$ch> ) {
chomp;
my ( $key, $value ) = split /,/;
$config{ $key } = $value;
}
close $ch;
[ Code reformatted for legibility. ]
#!/usr/bin/perl
The next two lines should be:
use warnings;
use strict;
so that perl will help you find mistakes in your code.
$outcome = validateFile();
if($outcome == 1) {print("file is not proper, Please check the
file\n");}
else {print("Everything is fine in a file\n");}
sub validateFile
{
my $state=0;
$ARGV[0]=ROUTER;
$ARGV[1]=5;
my $first=$ARGV[0];
my $second=$ARGV[1];
my $line_num = '';
my $fname="seed.csv";
open(OF, $fname) or die "Can't open user database file!\n";
You should include the $! variable and probably $fname in the error
message so you know *why* it failed.
$. = 0;
Why are you doing this? Hint: it has *no* effect on the value of $.
inside the loop.
LINE: while ($temp_line = <OF>) {
You only have one loop so you don't really need the label.
chomp $temp_line;
@array = split(/,/,$temp_line,-1);
#print "CURRENT LINE $temp_line\n";
if ($first eq $array[0])
{
#print("$first is found\n");
$arraylength=@array;
# check particular line
if($second ne $arraylength)
You are comparing two numbers so you should use a numerical compare.
Also you could use the array directly:
if ( $second != @array )
Another thing you could do is instead of chomping and splitting up the
line is to just count the number of commas plus one:
if ( $second != 1 + $temp_line =~ tr/,// )
{
print "CURRENT LINE $temp_line\n";
$line_num = $.;
print "LINE NUMBER $line_num\n";
Why not just:
print "LINE NUMBER $.\n";
print("Lenght of Current Line is $arraylength which
'Lenght' is usually spelt 'Length'.
is not matching with $second\n");
$state=1;
#last;
}
}
next LINE;
}
close($fname);
return ($state);
}
John
--
use Perl;
program
fulfillment
.
- References:
- Syntax Checker Script
- From: Nishant Lokre
- Syntax Checker Script
- Prev by Date: Re: Split function
- Next by Date: Re: Include variables from external perl script
- Previous by thread: Re: Syntax Checker Script
- Next by thread: perl with mysql which takes a file as a input that contains the info to create the tables
- Index(es):
Relevant Pages
|