×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# To eliminate repeated of an array

## To eliminate repeated of an array

(OP)
Exist some function to eliminate repeated elements of an Array ???

Example @array = ('a','b','a','x','x','b','c')

after function(\@array)

@array = ('a','b','x','c')

Thanks

### RE: To eliminate repeated of an array

I sometimes take a different approach to building the array......  Instead of using a simple list, I use a hash and as I get a new value I use it as a key who's value is '1'.

While (<INCOMING>) { $letters{$_} = 1; }

If any 'letter' repeats in <INCOMING>, it overwrites the previous instance in the hash
You can then, get your letters back with keys(%letters).

Or, you can use a variation on that trick against the array that you already have,

@array = ('a','b','a','x','x','b','c');

foreach $char (@array) {$letters{$char} = 1; } @unique_chars = sort(keys(%letters)); 'hope this helps.... keep the rudder amid ship and beware the odd typo ### RE: To eliminate repeated of an array Here's a short subroutine I wrote to do it: sub unique { my(@list) = @_; my %list = map {$_ => 1 } @list;

return sort keys %list;

} # unique

It takes an array (list) parameter and returns the same:

@nodupes = unique(@array);

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!