English 中文(简体)
缩略语
原标题:Perl, regex, extract data from a line

试图提取一条符合附件的线的一部分

use strict; 
use warnings;


# Set path for my.txt and extract datadir
my @myfile = "C:ackupsMySQLmy.txt";
my @datadir = "";

open READMYFILE, @myfile or die "Error, my.txt not found.
";
    while (<READMYFILE>) {
        # Read file and extract DataDir path
        if (/C:ackups/gi) {
        push @datadir, $_;
        }
    }

# ensure the path was found
print @datadir . " 
";

基本上首先试图确定我手提的档案的位置。 接下来想读一下,把部分线拉上。 错误是:

Unrecognized escape m passed through at 1130.pl line 17.

我请大家看。 在Perl的配对线之后,我怎么能 gr碎多个线子?,了解如何阅读档案和在档案中配对一线,然而,不能100%确保我能以最好的方式行使这一权利。 我也似乎产生错误:

Error, my.txt not found.

但是,档案确实存在于文件夹C中:

最佳回答

When Perl sees the string "C:ackupsMySQLmy.txt" it tries to parse any escape sequences, such as . But when it sees m in my.txt, it s an unrecognized escape sequence, hence the error.

解决这一问题的一个途径是适当逃脱你的反弹:>C:/backups/MySQL/my.txt>。 解决这一问题的另一个办法是使用单一报价而不是双重报价: C:ackupsMySQLmy.txt 。 <another way is to use the q()building:q(C:ackupsMySQLmy.txt).

问题回答

由于存在几个问题 我对我对以下法典所作的改动发表了意见。

use strict; 
use warnings;
# For pretty dumping of arrays and what not.
use Data::Dumper;

# Use single quotes so you don t have to worry about escaping   s.
# Use a scalar ($) instead of an array(@) for storing the string.
my $myfile =  C:ackupsMySQLmy.txt ;

# No need to initialize the array.
my @datadir;

# I believe using a scalar is preferred for file handles.
# $! will contain the error if we couldn t open the file.
open(my $readmyfile, $myfile) or die "error opening: $!";

while (<$readmyfile>) {
    # You must escape   s by doubling them.
    # If you are just testing to see if the line contains  c:ackups  you do not
    # need /g for the regex. /g is for repeating matches
    if (/C:\backups/i) {
        push(@datadir, $_);
    }
}

# Data::Dumper would be better for dumping the array for debugging.
# Dumper wants a reference to the array.
print Dumper(@datadir);

<>Update:

如果你再次提到数据的产出:假设,那么就算是阵列的表面代表。 如果你需要一种具体格式的产出,那么就必须加以编码。 开始时是:

print "$_
" for (@datadir);

1. 用前方弹而不是背心弹

是否应当使用<条码>日志而不是<条码>@myfile? 后者给你一个阵列,而且由于你在缩略语中重新提及,它实际上试图开一个称为ARRAY(0xdeadbeef)的“档案”,而不是实际档案。

文档之所以找不到,是因为你通过一个阵列来 open。 当它期待一个小数时,我猜测,正在粗略地对阵列进行评价,而不是作为名单,这样你实际上就能够说得 per,试图打开名字为1的档案,而不是我。 txt file.

相反的:

my $a =  filename ;
open FH, $a or die "Error, could not open $a: $!";
...

As other people have said, part of the issue is using " " rather than type of quoting. I try always to use unless I know I need to include an escape or interpolate a variable. Here are a number of pitfalls

    use 5.10.0 ;
    use warnings ;

    say "file is c:mydir" ;
    say "please pay $100 ";
    say "on VMS the system directory is sys$system" ;
    say "see you @5 ";

With double quotes

    Unrecognized escape m passed through at (eval 1) line 2.
    Possible unintended interpolation of @5 in string at (eval 1) line 5.
    file is c:mydir
    Use of uninitialized value $100 in concatenation (.) or string at (eval 1) line 3.
    please pay
    Use of uninitialized value $system in concatenation (.) or string at (eval 1) line 4.
    on VMS the system directory is sys
    see you

With single quotes

    file is c:mydir
    please pay $100
    on VMS the system directory is sys$system
    see you @5




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签