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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pushing unique data into arrays

Status
Not open for further replies.

jackz15

Programmer
Joined
Jun 28, 2006
Messages
103
Location
US
I know i can make sure things are unique if i just used a hash, but thats more of a hassle for me and puts a bit more work task for my program. So for example:
push @urls,$url;
how can i make sure that push only allows unique urls? or is this even possible?
 
you can loop through the array using grep:

Code:
push @array,$url if !grep(lc($_) eq lc($url),@array);

this will work OK as long as the arrays are not really big, meaning many thousands of elements in the array. For a few thousand elements or less it should be good enough if you don't want to use a hash instead.

Note my sample code is doing string checking (case insensitive) instead of pattern matching, this may not work for your purposes. If not you can switch to a regexp istead of using a string comparison operator.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top