如何删除具有特定字符的整个数组

时间:2019-04-24 02:34:22

标签: perl

我有一个从.xls文件提取的perl代码。我的.xls文件如下所示

NUMBER    NAME    ALPHABET
one       Jane        a
two       Adam        b
three     Josh        c
;four  
five      Agnes       e 
six       Mary        f
;seven 
eight     Lara        h        

我想提取信息,仅取第1列和第2列。我的perl代码如下。

     #!/usr/bin/perl

     use warnings;
     use strict;
     use Spreadsheet::ParseExcel;

     main ();

     sub main {

        my $filename = 'Book1.xls';
        my $parser   = Spreadsheet::ParseExcel->new();
        my $workbook = $parser->parse( $filename );

        if ( !defined $workbook ) {
           die "-E-: cannot parse <$filename>.\n ", $parser->error(), ".\n";
        }

        my $worksheet             = $workbook -> Worksheet ( 'a' ) || die "-E-: cannot parse family pin list.\n";   
        my ( $row_min, $row_max ) = $worksheet-> row_range();

        open ( my $file,"> output.txt");

        for my $row ( 1 .. $row_max ) {

           my @data;

           for my $col ( 0 ) {
              my $number = $worksheet-> get_cell( $row, $col );
              if ( $number ) {
                 push @data, $number-> value();
              }
              else {
                 push @data, '';
              }
            }

            for my $col ( 2 ) {
               my $alphabet = $worksheet->get_cell( $row, $col );
               if ( $alphabet ) {
                  push @data, $alphabet->value();
                  print $file  "@data\n";
               }
               else {
               push @data, '';
               }
            }     

     }

     close $file;

      print "done\n";
     }

结果是

    one a
    two b
    three c
    ;four 
    five e
    six f
    ;seven 
    eight h

我想删除以字符串“;”开头的整个数组。我像下面那样扩展代码

        open ( my $file,"> output.txt");

        for my $row ( 1 .. $row_max ) {

           my @data;

           for my $col ( 0 ) {
              my $number = $worksheet-> get_cell( $row, $col );
              if ( $number ) {
                 push @data, $number-> value();
              }
              else {
                 push @data, '';
              }
            }

            for my $col ( 11 ) {
               my $alphabet = $worksheet->get_cell( $row, $col );
               if ( $alphabet ) {
                  push @data, $alphabet->value();
               }
               else {
               push @data, '';
               }
            }


      my @new_data = grep(!/;/, @data);
      my @latest_data = grep ( $_ ne '', @new_data);
      print $file  "@latest_data\n";


     }

     close $file;

      print "done\n";
     }

输出结果如下。

     one a
     two b
     three c

     five e
     six f

     eight h

我不想成为空白。我要如何消除产生如下结果的空白空间?

     one a
     two b
     three c
     five e
     six f
     eight h

我也尝试这样做,但是结果是一样的。

     for my $index (reverse 0..$#data) {
        if ( $data[$index] =~ /^;/ ) {
        splice(@data, $index, 1);
       }
     }
     print $file "@data\n";

2 个答案:

答案 0 :(得分:0)

让我们掌握您的数据重点

@data=("one a", "two b","three c", ";four d","five e","six e","seven g","eight h")
for(@data){
s/^;.*//;              # // <-- Put Replacer here between `//` , leave it to remove line
push(@new_data,$_)}
print "$_\n" for @new_data

答案 1 :(得分:0)

我希望您正在寻找next

use warnings;
use strict;

@data=("one a", "two b","three c", ";four d","five e","six e","seven g","eight h")
for(@data){
    next if (/^;/);
    print $_,"\n";
}