viffer has asked for the wisdom of the Perl Monks concerning the following question:
I'm populating an array, which I then use in a foreach statement to print into an html table.
The issue I have is that I'm getting duplicates in the array, so basically I want to check if the item exists in the array and then only add the new record if it doesn't exist.
I've tried turning the array into a hash, which works, but it seems to be a waste of time to drop the array into a hash every single time I need to check it.
Is there a quicker, more efficient way to do it? I suspect using grep would be even slower.if ($server eq 'lsrch1') { my %params = map { $_ => 1 } @lsrch1; if(! exists($params{$string})) { push (@lsrch1, $string); } }
Thanks all
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Checking if an item exists in an array before adding a record to it.
by Old_Gray_Bear (Bishop) on Aug 10, 2013 at 15:32 UTC | |
|
Re: Checking if an item exists in an array before adding a record to it.
by AnomalousMonk (Archbishop) on Aug 10, 2013 at 18:26 UTC | |
|
Re: Checking if an item exists in an array before adding a record to it.
by RichardK (Parson) on Aug 10, 2013 at 15:17 UTC | |
|
Re: Checking if an item exists in an array before adding a record to it.
by Happy-the-monk (Canon) on Aug 10, 2013 at 15:19 UTC | |
|
Re: Checking if an item exists in an array before adding a record to it.
by Laurent_R (Canon) on Aug 10, 2013 at 19:15 UTC | |
|
Re: Checking if an item exists in an array before adding a record to it.
by kcott (Archbishop) on Aug 11, 2013 at 06:09 UTC |