Hi all,
I have written a perl script using two arrays...
but I couldnot access the all elements of the arrays. bellow is my code..
#!/usr/bin/perl
use strict;
my $server= "vsacentral";
my $dt = `date +%D`;
my $dt1= `date +%b" "%d`;
my $id = "";
my @cron_scripts = qw{ wms_vsa_load.ksh
visteon_vsa_load.ksh
philmor_vsa_load.ksh
ing_vsa_load.ksh
franklin_vsa_load.ksh
fluor_vsa_load.ksh
dow_vsa_load.ksh
disney_vsa_load.ksh
bp_vsa_load.ksh
belo_vsa_load.ksh};
my @log_files = qw{ wms_import.log
visteon_import.log
philmor_import.log
ing_import.log
franklin_import.log
fluor_import.log
dow_import.log
disney_import.log
bp_import.log
belo_import.log};
my $str=qq{
<html>
<body>
<table align=center style="font-family:verdana;font-size:12px;border:1px solid #000000 ;">
<tr>
<th>Server:$server</th>
<th>Date:$dt</th>
</tr>
</table>
<table align=center style="font-family:verdana;font-size:12px;border:1px solid #000000 ;">
<tr>
<th>Scheduled Date</th>
<th>Scheduled Run Time</th>
<th>Sheduled Job</th>
<th>Log Date</th>
<th>Log Time</th>
<th>Log File</th>
</tr>
};
for ( $id = 0; $id <=$#log_files; $id++){
my $s_job=shift@cron_scripts;
my $s_runtime=`crontab -l|grep -i $s_job|cut -c 1-5`;
my $log_file=shift@log_files;
my $log_date=`ls -l *im*.log|grep -i $log_file|tr -s ' '|cut -d ' ' -f 6-7`;
my $log_time=`ls -l *im*.log|grep -i $log_file|tr -s ' '|cut -d ' ' -f 8`;
$str.=qq{
<tr>
<td align=center>$dt1</td>
<td align=center>$s_runtime</td>
<td>$s_job</td>
<td>$log_date</td>
<td>$log_time</td>
<td>$log_file</td>
</tr>};
}
$str.=qq{
</table>
<br></br>
</body>
</html>};
print $str;
Plz help..
thanks in advance.
pludi
March 25, 2009, 7:50am
2
First, please use tags for source and listings. Easier to read means easier to understand means easier to correct.
Second, your for loop is off by one. $#array returns the index of the last element, eg. for an array with 3 elements it's 2. So your loop should be
for ( $id = 0; $id <=$#log_files+1; $id++){
or
for ( $id = 0; $id <=scalar(@log_files); $id++){
or using more efficient perl style loops:
for my $id (0 .. $#log_files){
It works as..
for ( $id = 0; $id <=$#log_files+9; $id++){
Thats quite odd, because that takes you past the end of the array. Why are you adding 9 to the last index?
vikalp
March 26, 2009, 1:50am
6
Hi,
Can you please check it again? I am not able to understand how $#log_files+9 is working
pludi
March 26, 2009, 2:32am
7
#!/usr/bin/perl
use strict;
my $server = "vsacentral";
my $dt = `date +%D`;
my $dt1 = `date +%b" "%d`;
my $id = "";
my @cron_scripts = qw{ wms_vsa_load.ksh
visteon_vsa_load.ksh
philmor_vsa_load.ksh
ing_vsa_load.ksh
franklin_vsa_load.ksh
fluor_vsa_load.ksh
dow_vsa_load.ksh
disney_vsa_load.ksh
bp_vsa_load.ksh
belo_vsa_load.ksh};
my @log_files = qw{ wms_import.log
visteon_import.log
philmor_import.log
ing_import.log
franklin_import.log
fluor_import.log
dow_import.log
disney_import.log
bp_import.log
belo_import.log};
my $str = qq{
<html>
<body>
<table align=center style="font-family:verdana;font-size:12px;border:1px solid #000000;">
<tr>
<th>Server:$server</th>
<th>Date:$dt</th>
</tr>
</table>
<table align=center style="font-family:verdana;font-size:12px;border:1px solid #000000;">
<tr>
<th>Scheduled Date</th>
<th>Scheduled Run Time</th>
<th>Sheduled Job</th>
<th>Log Date</th>
<th>Log Time</th>
<th>Log File</th>
</tr>
};
for ( $id = 0 ; $id <= $#log_files ; $id++ ) {
my $s_job = shift @cron_scripts;
my $s_runtime = `crontab -l|grep -i $s_job|cut -c 1-5`;
my $log_file = shift @log_files;
my $log_date =
`ls -l *im*.log|grep -i $log_file|tr -s ' '|cut -d ' ' -f 6-7`;
my $log_time = `ls -l *im*.log|grep -i $log_file|tr -s ' '|cut -d ' ' -f 8`;
$str .= qq{
<tr>
<td align=center>$dt1</td>
<td align=center>$s_runtime</td>
<td>$s_job</td>
<td>$log_date</td>
<td>$log_time</td>
<td>$log_file</td>
</tr>};
}
$str .= qq{
</table>
<br></br>
</body>
</html>};
print $str;
Here's the culprit. You're changing the size of the array you want to loop over within the loop. Either save the size of the array before you enter the loop and take that as your final value, or change the parts in red to read
my $s_job=cron_scripts[$id];
my $log_file=$log_files[$id];