James Edward Gray II wrote:
Gunnar Hjalmarsson wrote:

if ( grep /my string/, @myarray ) { some code }

Ordinarily, I wouldn't even mention this, but since the conversation is about what is fastest...

The OP was not about what is fastest at all. It was about whether there is "a better way" than first joining the elements, without any definition of "better".

The above is pretty much the textbook perfect example of an
inefficient use of grep().  The reason is that we just want to know
if @myarray contains ANY /my strings/, but grep() fetches them ALL.
If the list contains 10,000 entries, and the first is a /my
string/, that's 9,999 lookups we didn't need.

I didn't claim that grep() provides a fast solution. But it does provide a solution with a minimum of Perl code.

As regards efficiency, we were informed that the array contains the
output from some "show version" command, so it should be safe to
assume that we are not talking about a huge number of elements...

I would simply substitute List::Utils' first() in the above
example, to "fix" this.

Though again, it probably isn't "broken" at all.  (See my speed
rant in previous message.)

Yeah, I read it, and you did object to you own arguments, didn't you? ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to