简体   繁体   English

如何修改 perl 脚本以读取 excel 而不是 Html 文件

[英]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.这是否可能做到这一点,因为现在我有一个 perl 脚本,它读取 Html 文件并提取数据以显示在另一个 html 文件上。

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.很抱歉坦率地问这个问题,但由于我对 perl 很陌生,我必须接受这项任务,所以我在这里寻求一些有用的建议或建议来指导我完成这项任务。 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.恐怕如果您不了解该程序中发生的事情,并且根本不了解如何处理这样的任务,Stack Overflow 可能不是寻求帮助的正确地方。


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.使用perltidy缩进继承的代码,以便您可以更好地阅读它。 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 .您可以使用Test::More Another useful testing module here is Test::File .这里另一个有用的测试模块是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.还要在重构时添加use strictuse warnings以更轻松地捕获错误。 If stuff breaks because of that, make it work before you continue.如果事情因此而中断,请在继续之前使其工作。 Those pragmas tell you what's wrong.这些 pragma 会告诉您出了什么问题。 The most common one you will encounter is Global symbol "$foo" requires explicit package name .您将遇到的最常见的一种是全局符号“$foo”需要明确的包名称 That means you need to put my in front of the first assignment, or declare the variable before.这意味着您需要将my放在第一个赋值的前面,或者在此之前声明变量。

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.如果您对 Perl 的了解还不够多,那么如果您已经了解编程,那么请阅读 Curtis Poe 的Beginning Perl 之类的书。 If not, check out Learning Perl by Randal L. Schwartz.如果没有,请查看 Randal L. Schwartz 的Learning Perl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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