我可以在 Perl 中的数组上使用 Split() 吗?我正在寻找数组元素的正则表达式
我正在尝试获取本地计算机上图像文件的名称,并且不应重复。我已经在数组中获取了所有文件名,当我尝试进入数组并使用 Split() 时,我得到一些 888 作为输出。我正在寻找一个正则表达式,可以在遇到“_”(下划线)之前打印所有内容。我需要获取名称,然后可以使用 Uniq 删除重复项。欢迎任何建议。
我有以下代码:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use Data::Dumper;
use List::MoreUtils qw/ uniq /;
my $localdir = 'images/p/';
my @filefound;
find(
sub {push @filefound, $File::Find::name if /.jpg$/ },
$localdir
);
foreach (@filefound){
my @result = split('/images/p/',@filefound);
foreach (@result) { print "$_ \n";}
}
I am trying to get the names of the image files on my local machine and also that It should not be repeated. I have got all the file names in an array and when I try to go in the array and use Split() I get some 888 as output. I am looking for a regex where I can print everything before the '_'(underscore) encountered. I need to get the names and then I can use Uniq to remove the duplicates. Any suggestions are welcomed.
I have the following code :
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use Data::Dumper;
use List::MoreUtils qw/ uniq /;
my $localdir = 'images/p/';
my @filefound;
find(
sub {push @filefound, $File::Find::name if /.jpg$/ },
$localdir
);
foreach (@filefound){
my @result = split('/images/p/',@filefound);
foreach (@result) { print "$_ \n";}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在循环@filefound,但当前迭代不使用该元素。
并且您正在传递 File::Find 'images/p',因此您找到的名称不会以 '/images/p/' 开头。
尝试:
另外,你在 _ 之前说了一些关于一切的事情;这似乎与您的代码所做的完全不同。
如果您只需要完整路径的一部分,请考虑使用 File::Basename 。
You are looping over @filefound but then not using the element for the current iteration.
And you are passing File::Find 'images/p', so your found names are not going to start '/images/p/'.
Try:
Also, you say something about everything before the _; that doesn't appear to be anything like what your code is doing.
Consider using File::Basename if you need just part of a full path.