开发者

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

开发者 https://www.devze.com 2023-03-21 07:56 出处:网络
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 goi

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

精彩评论

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