Hi,
I have an array containing following sample information
@array = qw (chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07)
I need to replace all duplicate entries by an underscore to get the following output
@array = qw (chr02 _ _ _ _ chr03 _ chr04 _ chr05 _ _ chr07 _)
Can someone suggest how to get this as I found only how to delete/remove duplicate entries but not to replace.
Thanks in advance
Cheers:)
Try:
@array = map {$x=($x{$_})?"_":$_;$x{$_}=1;($x)} @array;
Hi Bartus, That worked nicely, cheers.
My real problem is about handling 2 arrays and making other arrays from them
For example
@arrchr = qw(chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07 chr07);
@arrcov = qw(45 86 112 62 107 50 94 72 65 89 95 119 97 109 100);
Now what I'm trying to do is to create series of arrays named by unique entries in @arrchr and fill it with corresponding values from @arrcov, so as to get the following output
@chr02 =qw (45 86 112 62 107) #first 5 entries from @arrcov,
@chr03 =qw (50 94) #next 2 entries from @arrcov,
@chr04 =qw (72 65) #next 2 entries from @arrcov,
@chr05 =qw (89 95 119) #next 3 entries from @arrcov,
@chr07 =qw (97 109 100) #last 3 entries from @arrcov
How do I do that ?? Can you please enlighten on this ? I need to make these arrays to use them to plot graphs using GD::Graph module.
Cheers and thanks for your help and comments always 
Good day !!
I think the best way to do it is to use hash of array references, instead of arrays. Try this code:
#!/usr/bin/perl
@arrchr = qw(chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07 chr07);
@arrcov = qw(45 86 112 62 107 50 94 72 65 89 95 119 97 109 100);
for ($i=0;$i<=$#arrchr;$i++){
push @{$h{"$arrchr[$i]"}}, $arrcov[$i];
}
print "chr02: @{$h{chr02}}\n";
print "chr03: @{$h{chr03}}\n";
You can then reference each of the "chr" elements using @{$h{chrXX}} syntax, so instead of @chr02 you should use @{$h{chr02}} and so on.
Thanks ever so much Bartus
You ROCK !!
Cheers
Good day 