Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing duplicates in an array

Status
Not open for further replies.

perln

Programmer
Dec 3, 2008
2
US
Hi everybody,
I seem to have hit a wall with my perl script. I have managed to get my data into a desired format besides one problem: duplicates.
I'm not trying to remove them, I'm trying to rename them so that I can export them as field names into mysql.
Basically if I have an array such as
@a = (a,b,c,a,d,a),
I'm trying to alter it ie
@a_new = (a,b,c,a1,d,a2)

If you can suggest me some pointers, I'd greatly appreciate it. Thanks!
 
my %known;
for my $num(@a) {
if ($known{$num}) {
$known{$num}++;
print "$num$known\n";
}
else {
$known{$num} = 0;
print "$num\n";
}
}

Try that..

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Here's one way:
Code:
my @array = qw/a b c a d a/;
my %seen;
my @array_new = map {$_ . ($seen{$_}++ ? $seen{$_}-1 : "")} @array;
 
Thank you travs69 & rharsh .... It's working now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top