简体   繁体   中英

How to modify a perl script to read excel instead of Html files

My first question is:

Is this possible to do this, since now I have a perl script which reads Html file and extract data to display on another html file.

If the answer for the question above is Yes, my second question would be:

How to do this?

Sorry to ask frankly as this, but since I'm so new for perl, and I have to take this task, so I'm here for some useful advice or suggestion to guide me through this task. Appreciate your help in advance.

Here's a part of the code, since the whole chunk is quite long:

$date=localtime();
($TWDAY, $TMTH, $TD1D, $TSE, $TYY) = split(/\s+/, $date);
$TSE =~ s/\://g;
$STAMP=_."$TD1D$TMTH$TYY";

@ServerInfo=();

#---------------------------------------------------------------------------   -------------------------------
# Read Directory
#----------------------------------------------------------------------------------------------------------   
$myDir=getcwd;

#---------------------------------------------------------------------------------------------------------- 
# INITIALIZE HTML FORMAT
#----------------------------------------------------------------------------------------------------------  
&HTML_FORMAT;
#---------------------------------------------------------------------------------------------------------- 
# REPORT 
#----------------------------------------------------------------------------------------------------------  
if (! -d "$myDir/report") { mkdir("$myDir/report");};

$REPORTFILE="$myDir/report/checkpack".".htm";

open OUT,">$REPORTFILE" or die "\nCannot open out file $REPORTFILE\n\n";
print OUT "$Tag_Header";
#----------------------------------------------------------------------------------------------------------  

sub numSort {
if ($b < $a) { return -1; }
elsif ($a == $b) { return 0;}
elsif ($b > $a) { return 1; }
}

@ArrayDir = sort numSort @DirArray;

#while (<@ArrayDir>) {

@OutputDir=grep { -f and -T } glob "$myDir/*.htm $myDir/*.html";
#}

#----------------------------------------------------------------------------------------------------------

@ReadLine3=();

$xyxycnt=0;


foreach $InputFile (@OutputDir) { #---- MAIN

$filename=(split /\//, $InputFile) [-1]; print "-"x80 ; print     "\nFilename\t:$filename\n";

open IN, "<$InputFile" or die "Cannot open Input file $InputFile\n";
@MyData=();
$DataCnt=0;
@MyLine=();
$MyLineCnt=0;

while (<IN>) {
$LINE=$_;
chomp($LINE);

$LINE=~s/\<br\>/XYXY/ig;
$LINE=~s/\<\/td\>/ \nXYZXYZ\n/ig;

$LINE=~s/\<dirname\>/xxxdirnameyyy/ig;

$LINE=linetrim3($LINE);
$LINE=linetrim($LINE);

$LINE=~s/XYXY/\<br\>/ig;

$LINE=~s/xxxdirnameyyy/&lt dirname &gt/ig;

$LINE=~s/^\s+//ig;
print OUT2 "$LINE\n";

if (defined($LINE)) { $MyData[$DataCnt]="$LINE"; $DataCnt++ ; }

}

close IN;

foreach $ReadFile (@MyData) {  #--- Mydata
$MyLineCnt++;
$MyLine[$MyLineCnt]="";

#### FILENAME
$ServerInfo[0]="$filename";

#### IP ADDRESS
if ($ReadFile =~ /Host\/Device Name\:/)     { 
    #print "$ReadFile\n"
    ($Hostname)=(split /\:|\s+/, $ReadFile)[3]; print "$Hostname\n";
    &myServerInfo("$Hostname","1");

}
if ($ReadFile =~ /IP Address\(es\)/)        {@ListIP=(); $SwIP=1; $CntIP=0 ; };


#### OPERATING SYSTEM & VERSION
if ($ReadFile =~ /Operating System\:/)      { 
    $SwIP=0;
    $OS=    (split /\:|\s+/, $ReadFile)[3]; &myServerInfo("$OS","3") ; print "$OS\n";
    $OSVer= (split /\:|\s+/, $ReadFile)[-2]; &myServerInfo("$OSVer","4") ; print "$OSVer\n";

};

#### GET IP VALUE
if ($SwIP==1) {
    $ReadFile=(split /\:/,$ReadFile) [2];
    $ReadFile=~s/[a-z|A-Z]|\(|\)|\// /ig; print "$ReadFile\n";
    if ($CntIP==0) {
        #$ListIP[$CntIP]=(split /\s+/,$ReadFile) [1];
        @ListIP="$ReadFile";

    } elsif ($CntIP==1) { print "\n\t\t  $ReadFile\n" ; $ListIP[$CntIP]="\n$ReadFile"; 
    } else  { print "\t\t  $ReadFile\n" ; $ListIP[$CntIP]="\n$ReadFile"; };
     $CntIP++;
}

I'm afraid if you don't understand what is going on in this program and you also don't understand how to approach a task like this at all, Stack Overflow might not be the right place to get help.


Let me try to show you the approach I would take with this. I'm assuming there is more code.

  • First, write down a list of everything you know:
    • What is the input format of the existing file
    • Where does the existing file come from now
    • What is the output format of the existing file
    • Where does the generated output file go afterwards
    • What does the new file look like
    • Where does the new file come from
  • Use perltidy to indent the inherited code so you can read it better. The default options should be enough.
  • Read the code, take notes about what pieces do what, add comments
  • Write a unit test for the desired output format. You can use Test::More . Another useful testing module here is Test::File .
  • Refactor the part that generated the output format to work with a certain data structure. Use your tests to make sure you don't break it.
  • Write code to parse the new file into the data structure from the point above. Now you can plug that in and get the expected output.
  • Refactor the part that takes the old input file from the existing file location to be a function, so you can later switch it for the new one.
  • Write code to get the new file from the new file location.
  • Document what you did so the next guy is not in the same situation. Remember that could be you in half a year.

Also add use strict and use warnings while you refactor to catch errors more easily. If stuff breaks because of that, make it work before you continue. Those pragmas tell you what's wrong. The most common one you will encounter is Global symbol "$foo" requires explicit package name . That means you need to put my in front of the first assignment, or declare the variable before.

If you have specific questions, ask them as a new question with a short example. Read how to ask to make sure you will get help on those.

Good luck!


After seing your comment I am thinking you want a different input and a different output. In that case, disregard this, throw away the old code and start from scratch. If you don't know enough Perl, get a book like Curtis Poe's Beginning Perl if you already know programming. If not, check out Learning Perl by Randal L. Schwartz.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM