简体   繁体   English

Perl +如何在foreach循环中使用chomp

[英]perl + how to use chomp in foreach loop

the following perl script (Showing down) 以下perl脚本(显示如下)

print the following XML file but the XML have problem because the unnecessary "&#10" char after TEST word 打印以下XML文件,但XML存在问题,因为在TEST词后添加了不必要的“&#10”字符

 <ROOT>
     <FILE Name="TEST1&#10;"/>
     <FILE Name="TEST2&#10;"/>
     <FILE Name="TEST&#10;"/>ar
</ROOT>

I think the "&#10" characters its because the linebreak (read the output of a command into @myNames with the backtick operator) it seems that this problem of existing "&#10" in my XML can be solved chomp command 我认为“&#10”字符是因为换行符(使用反引号运算符将命令的输出读取到@myNames中)似乎可以解决我的XML中现有的“&#10”问题。

my question: in which way need to add the chomp command in my script in order to solve the problem 我的问题:以哪种方式需要在脚本中添加chomp命令以解决问题

my perl script 我的Perl脚本

#!/usr/bin/perl

use warnings;
use XML::LibXML;


my $doc  = XML::LibXML::Document->new; 
my $root = $doc->createElement('ROOT');
$doc->setDocumentElement($root);


my @myNames=` ls /var/tmp | grep FILE.xml | sed "s/.FILE.*//"  `;

foreach (@myNames) {

my $objVar = $doc->createElement('FILE');
$root->appendChild($objVar);
$objVar->setAttribute('Name' , $_ );

} 


my @lines = split /\n/, $doc->toString(1);
shift @lines;

foreach (@lines) {

print "$_\n";
}

the files under /var/tmp / var / tmp下的文件

   TEST1-FILE.xml
   TEST2-FILE.xml
   TEST-FILE.xml

Go ahead and use it immediately after you get the list of filenames: 得到文件名列表后,请立即使用它:

my @myNames=` ls /var/tmp | grep FILE.xml | sed "s/.FILE.*//"  `;
chomp(@myNames);
...

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

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