我有一个包含文本文件数据的数组。
我想过滤数组并将一些信息复制到另一个数组。 grep
似乎不起作用。
这就是我所拥有的
$file = 'files.txt';
open (FH, "< $file") or die "Can't open $file for read: $!";
@lines = <FH>;
close FH or die "Cannot close $file: $!";
chomp(@lines);
foreach $y (@lines){
if ( $y =~ /(?:[^\\]*\\|^)[^\\]*$/g ) {
print $1, pos $y, "\n";
}
}
public_html
Trainings and Events
General Office\Resources
General Office\Travel
General Office\Office Opperations\Contacts
General Office\Office Opperations\Coordinator Operations
public_html\Accordion\dependencies\.svn\tmp\prop-base
public_html\Accordion\dependencies\.svn\tmp\props
public_html\Accordion\dependencies\.svn\tmp\text-base
正则表达式应该取最后一个或两个文件夹并将它们放入自己的数组中进行打印。
答案 0 :(得分:6)
正则表达式对此非常挑剔。将路径拆分为组件然后根据需要进行计数要容易得多。还有一个适合您目的的工具,核心模块File::Spec
,正如xxfelixxx在评论中提到的那样。
您可以使用其catdir
来分解路径,并使用use warnings 'all';
use strict;
use feature 'say';
use File::Spec::Functions qw(splitdir catdir);
my $file = 'files.txt';
open my $fh, '<', $file or die "Can't open $file: $!";
my @dirs;
while (<$fh>) {
next if /^\s*$/; # skip empty lines
chomp;
my @all_dir = splitdir $_;
push @dirs, (@all_dir >= 2 ? catdir @all_dir[-2,-1] : @all_dir);
}
close $fh;
say for @dirs;
来构建路径。
use strict
我使用模块的功能界面,而对于较重的工作,你希望它的面向对象。将整个文件读入数组有其用途,但一般是逐行处理。列表操作可以更优雅地完成,但我只是为了简单。
我想补充一些一般性意见
始终使用use warnings
和my $fh
使用词法文件句柄,FH
代替\
了解(至少)十二个或两个最常用的模块真的有帮助。例如,在上面的代码中,我们甚至不必提及分隔符tutorial = open('c:/test/z.txt','r')
## Input file looks like
>qrst
ABCDE-- 6 6 35 25 10
>qqqq
ABBDE-- 7 7 28 29 2
org = []
seqlist = []
seqstring = ""
for line in tutorial:
if line.startswith(">"):
if seqstring!= "":
seqlist.append(seqstring)
seqstring = ""
org.append(line.rstrip("\n"))
else:
seqstring += line.rstrip("\n")
seqlist.append(seqstring)
l = seqlist
#print l
j = []
ll = len(seqlist)
for i in range(0,ll):
sq = l[i]
sequence = sq.split(" ")[0] ## Stores only the alphabets
qualities = sq.split(" ")[1:] ## Stores only the numeric
qualities = filter(None, qualities)
for sub in sequence:
if sub == "-": ## If sequences have "-", it inserts a "0" in that position in corresponding number
idx = list(sequence).index(sub)
qualities.insert(idx,"0")
# Error in the steps below
pairs = []
for sub in l:
print sub
new_list = []
for x in range(len(sequence)):
print x
new_tuple = (sequence[x], qualities[x]) #Printing this step, notice that only one of the sequences is printed twice. ERROR
print new_tuple
if int(qualities[x]) < 10:
new_tuple = ("Z", qualities[x])
new_list.append(new_tuple)
pairs.append(new_list)
print pairs
# When I print pairs it looks like this: [[('Z', '7'), ('Z', '7'), ('B', '28'), ('D', '29'), ('Z', '2'), ('Z', '0'), ('Z', '0')], [('Z', '7'), ('Z', '7'), ('B', '28'), ('D', '29'), ('Z', '2'), ('Z', '0'), ('Z', '0')]]
# Sequence#2 is printed twice over. The first one is not taken in
。
答案 1 :(得分:1)
我无法写完整的答案,因为我正在使用手机。无论如何, zdim 主要回答你。但我的解决方案看起来像这样
delegate: Component
{
Rectangle
{
width: 250
height: textItem.height
Text {
id: textItem
text: host + ' ' + status + ' (' + displayRole +')'
}
}
}