I have an array@array1={"abc","abc","xyz","uvw","uvw","uvw"}now ,what regular expression can I use if I want @array1 to become@array2={"abc","xyz","uvw"}
as suggested by Achin you can use this to remove duplicates@array1 = keys map{ $_ => "" } @array1;
The textfile holds the code and the jpg file is the error message i got after running that.My code was generating paths of some files(@split_main_path={"default/main/WORKAREA/wa1/folder1/file.html" , "default/main/WORKAREA/wa1/folder1/software.html " , "default/main/WORKAREA/wa1/folder2/home.html").I poped out the files.So I was left with the paths to the parent folders of the files(@split_main_path= "default/main/WORKAREA/wa1/folder1","default/main/WORKAREA/wa1/folder1" , "default/main/WORKAREA/wa1/folder2"). Now I want to remove the repeated "default/main/WORKAREA/wa1/folder1" from the above list.Please suggest.
How can I re-build the paths from the array that holds the split path?
I hope you have split the path by using / as a separator, which in turn becomes an array. To join the elements of an array... can use the following command:my $fullPath = join ('/', @arrName);
please tell something for the #3 thread window.
my @array = ('10', '11', '12', '10', '12', '13', '14');my %hash = map {$_,''} @array;@array = sort(keys %hash);
Just try below@array1 = grep(!$seen{$_}++,@array1);Hope this will help youThanks Nicholas
It works, but can you please explain a bit on it?
I am gettin this error message. That % seen requires explicit package name. I tried giving my $seen to solve the issue. But that didnt help.What should i do?
It worked fine with me. You cannot declare it as a variable.Just remove the my $seen;it should be fine then. Can you please write the code using for it ?
sub hash_grep{ my $full_list = shift; my %seen; my @new_list = grep(!$seen{$_}++, @$full_list); return \@new_list;}
sub simple_hash{ my $full_list = shift; my %seen = map {$_, q{}} @$full_list; my @new_list = keys(%seen); return \@new_list;}
Benchmark: timing 10000 iterations of HASH_GREP, SIMPLE_HASH... HASH_GREP: 1 wallclock secs ( 0.95 usr + 0.01 sys = 0.96 CPU) @ 10416.67/s (n=10000)SIMPLE_HASH: 4 wallclock secs ( 3.87 usr + 0.05 sys = 3.92 CPU) @ 2551.02/s (n=10000)