Perl 程序不打印输出..

发布于 2024-12-10 11:21:06 字数 4245 浏览 0 评论 0 原文

要对程序进行哪些更改,以便从第一个文本文件读取,将输出打印到其他文本文件中。 目前,它没有在其他文本文件中提供输出,而是从第一个文本文件读取。

我认为程序在某种程度上是正确的,但仍需要经过审查......

Program is as follows :
    #--------------------------------------------------------------------------------------------------#
    #     This program is intended for storing the sentences in a text file with following conditions: #
    #   1. The first letter of a sentence in upper case.                       #
    #   2. As sentence completes at [. or ? or !], the succeeding character i.e. beginning     #
    #      character of other sentence in upper case.                          #
    #   3. There may be combinations of more than one [. or ? or !] "WITH Spaces".         #
    #--------------------------------------------------------------------------------------------------#

    #!/usr/bin/perl -w

    use strict;

    #----------------------------------------------------------------------------------#
    #     "File::Slurp" used for reading whole file by use of a scalar variable        #
    #----------------------------------------------------------------------------------#
    use File::Slurp;

    #----------------------------------------------------------------------------------#
    #     "File::Slurp qw( :all)" used for getting all subs in the module exported     #
    #----------------------------------------------------------------------------------#
    use File::Slurp qw( :all );

    #======== BLOCK-1 ==========#
    #----------------------------------------------------------------------------------#
    #                 Opens File if exists else file could not open                    #
    #----------------------------------------------------------------------------------#
    open (FILE, 'matter.txt') || die("Could not open file!");

    #----------------------------------------------------------------------------------#
    #   "File::Slurp" used : by "read_file" AND "write_file" functions,        #
    #           using scalar variable                      #
    #----------------------------------------------------------------------------------#
    my $LowText_1 = read_file( 'matter.txt' );  #==== (R1_orig)

    my $UpText;
    my $ch;

    my $i=0;

    while($i < eof(FILE))       ## BLOCK-1 + BLOCK-2 (START)
    {
    #======== BLOCK-1 ==========#
     while($i < eof(FILE))
     {
        if($ch eq " ")
        {
        $ch = uc($ch);
        my $UpText = write_file('UpFirst_matter.txt', $ch); #==== (2)
        print "$UpText\n";
        my $LowText_2 = read_file( 'matter.txt' );  #==== (R2)
        }
        else
        {
        my $UpText = write_file('UpFirst_matter.txt', $ch);
        my $LowText_1 = read_file( 'matter.txt' );  #==== (R1)
        }
     }$i++;     #==== (4)

     close(FILE) || die("Could not close file!!");

    #======== BLOCK-2 ==========#
     #----------------------------------------------------------------------------------#
     #                 Opens File if exists else file could not open                    #
     #----------------------------------------------------------------------------------#
     open (FILE, 'matter.txt') || die("Could not open file!");

     #----------------------------------------------------------------------------------#
     #  "File::Slurp" used : by "read_file" AND "write_file" functions,         #
     #          using scalar variable                       #
     #----------------------------------------------------------------------------------#
     my $LowText_2 = read_file( 'matter.txt' ); #==== (R2_orig)

    #======== BLOCK-2 ==========#
     while($i < eof(FILE))
     {
        if($ch eq "." || $ch eq "?" || $ch eq "!")
        {
        my $ch = uc($ch);
        my $UpText = write_file('UpFirst_matter.txt', $ch); #==== (3(x))
        print "$UpText";
        my $LowText_1 = read_file( 'matter.txt' );  #==== (R1)
        }
        else
        {
        my $UpText = write_file('UpFirst_matter.txt', $ch);
        my $LowText_2 = read_file( 'matter.txt' );  #==== (R2)
        }
     }$i++;     #==== (4)

    close(FILE) || die("Could not close file!!");   #==== (4_end)
    }$i++;              ## BLOCK-1 + BLOCK-2 (END)

What changes is to be made to the program so as to print the output in other text file, reading from first text file.
Currently it is not giving the output in other text file, reading from first text file.

I think program is correct upto some extent but still needs to be go through Review...

Program is as follows :
    #--------------------------------------------------------------------------------------------------#
    #     This program is intended for storing the sentences in a text file with following conditions: #
    #   1. The first letter of a sentence in upper case.                       #
    #   2. As sentence completes at [. or ? or !], the succeeding character i.e. beginning     #
    #      character of other sentence in upper case.                          #
    #   3. There may be combinations of more than one [. or ? or !] "WITH Spaces".         #
    #--------------------------------------------------------------------------------------------------#

    #!/usr/bin/perl -w

    use strict;

    #----------------------------------------------------------------------------------#
    #     "File::Slurp" used for reading whole file by use of a scalar variable        #
    #----------------------------------------------------------------------------------#
    use File::Slurp;

    #----------------------------------------------------------------------------------#
    #     "File::Slurp qw( :all)" used for getting all subs in the module exported     #
    #----------------------------------------------------------------------------------#
    use File::Slurp qw( :all );

    #======== BLOCK-1 ==========#
    #----------------------------------------------------------------------------------#
    #                 Opens File if exists else file could not open                    #
    #----------------------------------------------------------------------------------#
    open (FILE, 'matter.txt') || die("Could not open file!");

    #----------------------------------------------------------------------------------#
    #   "File::Slurp" used : by "read_file" AND "write_file" functions,        #
    #           using scalar variable                      #
    #----------------------------------------------------------------------------------#
    my $LowText_1 = read_file( 'matter.txt' );  #==== (R1_orig)

    my $UpText;
    my $ch;

    my $i=0;

    while($i < eof(FILE))       ## BLOCK-1 + BLOCK-2 (START)
    {
    #======== BLOCK-1 ==========#
     while($i < eof(FILE))
     {
        if($ch eq " ")
        {
        $ch = uc($ch);
        my $UpText = write_file('UpFirst_matter.txt', $ch); #==== (2)
        print "$UpText\n";
        my $LowText_2 = read_file( 'matter.txt' );  #==== (R2)
        }
        else
        {
        my $UpText = write_file('UpFirst_matter.txt', $ch);
        my $LowText_1 = read_file( 'matter.txt' );  #==== (R1)
        }
     }$i++;     #==== (4)

     close(FILE) || die("Could not close file!!");

    #======== BLOCK-2 ==========#
     #----------------------------------------------------------------------------------#
     #                 Opens File if exists else file could not open                    #
     #----------------------------------------------------------------------------------#
     open (FILE, 'matter.txt') || die("Could not open file!");

     #----------------------------------------------------------------------------------#
     #  "File::Slurp" used : by "read_file" AND "write_file" functions,         #
     #          using scalar variable                       #
     #----------------------------------------------------------------------------------#
     my $LowText_2 = read_file( 'matter.txt' ); #==== (R2_orig)

    #======== BLOCK-2 ==========#
     while($i < eof(FILE))
     {
        if($ch eq "." || $ch eq "?" || $ch eq "!")
        {
        my $ch = uc($ch);
        my $UpText = write_file('UpFirst_matter.txt', $ch); #==== (3(x))
        print "$UpText";
        my $LowText_1 = read_file( 'matter.txt' );  #==== (R1)
        }
        else
        {
        my $UpText = write_file('UpFirst_matter.txt', $ch);
        my $LowText_2 = read_file( 'matter.txt' );  #==== (R2)
        }
     }$i++;     #==== (4)

    close(FILE) || die("Could not close file!!");   #==== (4_end)
    }$i++;              ## BLOCK-1 + BLOCK-2 (END)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑丑阿 2024-12-17 11:21:06

这是一种方法:

#!/usr/local/bin/perl 
use strict;
use warnings;
use File::Slurp;

my $content = read_file('x.txt');
$content =~ s/([.!?]\s*|^)(\pL)/$1\U$2/g;
write_file('y.txt', $content);

Here is a way to do it:

#!/usr/local/bin/perl 
use strict;
use warnings;
use File::Slurp;

my $content = read_file('x.txt');
$content =~ s/([.!?]\s*|^)(\pL)/$1\U$2/g;
write_file('y.txt', $content);
回忆凄美了谁 2024-12-17 11:21:06
  • 使用 File::Slurp 两次是多余的。删除最不具体的一个(第一个)
  • open 的 2 个参数形式已经被弃用很长时间了。使用 3-arg 形式:

    我的$文件;
    open ($file, '<', 'matter.txt') or die;

  • 摘自File::Slurp<的文档/a> 你甚至不需要 open() 文件:

    # 将整个文件读入行数组 my @lines = read_file('filename');

  • Perl 自己的文档 open() 有很多示例,您可以从那里复制典型的文件读取循环

  • 如果您确实使用文档中所写的文件slurp,您可以立即写出“整个文件”(!):

    # 从行数组中写出整个文件 write_file( 'filename', @lines ) ;

  • 所以你需要

    1. 使用 File::Slurp 来read_file()整个文件,然后迭代数组来修改内容,然后write_file()一次整个数组,或
    2. 不要使用 File::Slurp,只打开两个文件(一个带有“<”用于读取,一个带有“>”用于写入)
  • File::Slurp可能更容易自己写(不是真的),但只使用 open、while 和 print 自己的优点是一次只需要在内存中保留一行。如果您要处理千兆字节的文件,这会产生巨大的差异。

  • 不要认为这是针对您个人的,但我认为您需要自己进行更多研究,并在询问其他人之前使用标准文档。

  • 作为此类情况下的一般建议,请从较小的示例开始。首先解决仅打开文件并将其内容打印在标准输出上的问题。然后添加您的输入处理 - 对内容的任何更改,或省略某些行,插入新行,等等。最后添加代码以将新行写入另一个文件。通过这种循序渐进的方法,您不会看到一堆问题,而是可以一次解决一个小问题。

祝你好运!

  • use'ing File::Slurp twice is redundant. Remove the one that's most unspecific ( the first one)
  • 2 argument form of open is deprecated for a long time already. Use 3-arg form:

    my $file;
    open ($file, '<', 'matter.txt') or die;

  • From the documentation of File::Slurp you don't even need to open() the file:

    # read in a whole file into an array of lines my @lines = read_file('filename');

  • Perl's own documentation of open() has lots of examples and you can copy a typical file readin loop from there

  • If you do use file slurp as written in the documentation you can write out "the whole file" at once(!) with

    # write out a whole file from an array of lines write_file( 'filename', @lines ) ;

  • So you need to

    1. use File::Slurp to read_file() the whole file, then iterate over the array to modify the content, then write_file() the whole array at once, or
    2. do not use File::Slurp and just open the two files (one with '<' for reading, one with '>' for writing)
  • File::Slurp may be easier to write (not really), but just using open, while and print yourself has the advantage that you need to keep only one line at a time in memory. This makes a huge difference if you're processing gigabytes of files.

  • Do not take this personally, but I think you need to research more yourself, and work with the standard documentation before asking other people.

  • As a general advice in situations like these, start with smaller examples. First tackle the problem of just opening a file and printing it's content on stdout. Then add your processing of input - any changes to the content, or omitting some lines, inserting new lines, whatever. Finally add the code to write the new lines into another file. With this step-by-step approach you're not looking at a pile of problems, but can solve one small problem one at a time.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文