开发者

I am unable to remove duplicates from my array , just getting the last element of the array

I know there is a similar question but I took tips from there and applied to my code to remove the duplicates. All I am getting is just the last element of the array. I am unable to see where I am going wrong. please help me find out where a I am going wrong.

use strict;
use warnings;
use File::Find;
use Data::Dumper;
use List::MoreUtils qw/ uniq /;

my $localdir = 'images/p/';
my @filefound;
my @split1;
my $before;

find(sub {push @filefound, $File::Find::name if /.jpg$/ },$localdir);

for(@filefound) { pr开发者_开发技巧int "$_ \n";}

foreach (@filefound){
my @result = split('_',$_);
@split1 = $result[0];
}

my %unique = ();
foreach my $item (@split1)
{ 
    $unique{$item} ++;
}

my @myuniquearray = keys %unique;
foreach (@myuniquearray){ print "$_  \n";}


The problem lies here:

foreach (@filefound){
    my @result = split('_',$_);
    @split1 = $result[0];
}

You are reassigning @split1 each time through the loop. Try push @split1, $result[0]; instead to push the new element onto the existing list.


In the first foreach you are overwriting @split1 in every iteration instead of adding the result to a list.


@split1 = $result[0]; You get only one element in your array there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜