On Aug 24, 2004, at 7:48 AM, Darren Birkett wrote:

OK, to be more specific, I'm using Net::Telnet:Cisco. When logged onto
a device I'm using the "show version" command, and storing the output in
an array. Each element of the array will therefore contain lots of
rubbish. I just want to match certain keyword(s) to determine device
type.

That sounds like a Regular Expression, naturally.

I guess a for loop is the closest I'm going to get. I just thought there would be a specific function for this.

If you need only to find the first occurrence, John's suggestion is perfect. You could also use:


use List::Util qw/first/;
if (first { /test/ } @list) {
        # ...
}

If you need all entries that match grep() is the right choice, in my opinion.

So then I wonder - which is more efficient.  My earlier "join" example,
or a for loop?

Does it matter? Will you have 10 million or more entries in this list? Do you need the answer in microseconds? Will the results be used in the guidance system of a nuclear missile?


Silly questions, I know, but often so is worrying about performance. With reasonable data on modern hardware, both will likely be faster than you blink. That's generally good enough, don't you think? My rule for optimization is, "Speed it up when it gets too slow".

Let me ask you a question: Which method is easier for you to understand? The answer to that is a much more noble pursuit, I think.

To me, join() is for strings and we're dealing with a list. Because of that, I would use list tools: for, grep(), first(), etc. That doesn't make me right though. That's just what makes sense to me, so that's what I would use.

Hope that helps.

James


-- 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