![]() |
in_array Functionality in Perl
On 21st February, 2008
|
Recent Articles
Similar Articles
One of the strengths of PHP when it comes to arrays is considered to be the in_array function (http://php.net/in_array), this function takes two parameters; the array and the item to be searched in the array and then returns a boolean result whether the item exists in the array. Perl inherently lacks this functionality, even popular Perl modules like List::Util lack such a function/method. As it was said by Plato once, "Necessity... the mother of invention.", I required to check whether a name existed in an array, bored by using conventional method of iterating the array every time I need to check, I wrote a simple sub-routine to get the job done. This sub-routine take two arguments, a reference to the array to be searched, and the item that needs to be searched. Simple enough the sub-routine works like magic, even my friend have found it very useful. I thought may be someone might find it useful, here's it, take a look. Code: Perl
Sample Usage Code: Perl
Output : Code:
[pradeep@go4expert ~]$ ./amlan_test.pl Hurray! Amlan you are in!! [pradeep@go4expert ~]$ |
|
|
#2 |
|
Go4Expert Member
![]() Join Date: Apr 2007
Location: Lisbon, Portugal
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
Re: in_array Functionality in Perl |
|
|
|
|
|
#3 |
|
Team Leader
![]() |
Re: in_array Functionality in Perl
Cool! I guess the first one you suggested would be more efficient than your 2nd suggestion and my sub-routine. What say?
__________________
Vote for the Most Entertaining Member of 2008 To err is human,to detect is divine! |
|
|
|
|
|
#4 |
|
Go4Expert Member
![]() Join Date: Apr 2007
Location: Lisbon, Portugal
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
Re: in_array Functionality in Perl
Lets time this Code: Perl
and the results in my machine are: Code:
Benchmark: timing 100000 iterations of in_array_1, in_array_2, in_array_pradeep... in_array_1: 5 wallclock secs ( 5.34 usr + 0.00 sys = 5.34 CPU) @ 18726.59/s (n=100000) in_array_2: 5 wallclock secs ( 4.48 usr + 0.00 sys = 4.48 CPU) @ 22321.43/s (n=100000) in_array_pradeep: 32 wallclock secs (32.13 usr + 0.00 sys = 32.13 CPU) @ 3112.36/s (n=100000) The solution of pradeep is very slow. This seems obvious since he is creating a big structure in memory. Now the other two are interesting. The grep is fast since runs in C code inside the machine, but has to check all the elements. The foreach stops when finds the first matching, so just half the match are needed in average.
|
|
|
|
|
|
#5 |
|
Team Leader
![]() |
Re: in_array Functionality in Perl
Hi oleber, I saw your post today! Good finding, even I was wondering that my sub-routine wasn't very efficient. And today only I figured out that using grep would be better, but I see that you have already posted it :-)
__________________
Vote for the Most Entertaining Member of 2008 To err is human,to detect is divine! |
|
|
|
![]() |
|
| Currently Active Users Reading This Article: 1 (0 members and 1 guests) | |
| Article Tools | Search this Article |
| Display Modes | |
| Bookmarks | |
|
|
|
|||||||||||||||||||||||||||||||||||||