Can I use Split() on an array in Perl ? I am looking for a regex on the array elements
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 (@filefo开发者_Python百科und){
my @result = split('/images/p/',@filefound);
foreach (@result) { print "$_ \n";}
}
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:
my @result = split('images/p/', $_);
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.
精彩评论