Re: searching a whole array without using a loop
[EMAIL PROTECTED] (John W. Krahn) wrote in news:[EMAIL PROTECTED]: If all the elements are unique then use a hash. if ( exists $hash{ 'my string' } ) { # do something } The most efficient way to determine if an element exists in an array is to use a for loop and exit early: my $found = 0; for ( @array ) { if ( $_ eq 'my string' ) { $found = 1; last; } } If you need to determine the number of matches you can use grep: my $count = grep $_ eq 'my string', @array; John 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. I guess a for loop is the closest I'm going to get. I just thought there would be a specific function for this. So then I wonder - which is more efficient. My earlier join example, or a for loop? Thanks for all the input Darren -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: searching a whole array without using a loop
Darren Birkett wrote: I just want to match certain keyword(s) to determine device type. I guess a for loop is the closest I'm going to get. I just thought there would be a specific function for this. Isn't grep() specific enough? Initially you said: $line = join(' ',@myarray); if ($line =~ /my string/) { some code } The equivalent using grep() would be: if ( grep /my string/, @myarray ) { some code } So then I wonder - which is more efficient. My earlier join example, or a for loop? To get the answer to such a question, do a benchmark with help of the Benchmark module. -- 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
Re: searching a whole array without using a loop
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
Re: searching a whole array without using a loop
On Aug 24, 2004, at 9:14 AM, Gunnar Hjalmarsson wrote: Isn't grep() specific enough? Initially you said: $line = join(' ',@myarray); if ($line =~ /my string/) { some code } The equivalent using grep() would be: if ( grep /my string/, @myarray ) { some code } Ordinarily, I wouldn't even mention this, but since the conversation is about what is fastest... 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 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.) James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: searching a whole array without using a loop
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
Re: searching a whole array without using a loop
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. I guess a for loop is the closest I'm going to get. I just thought there would be a specific function for this. So then I wonder - which is more efficient. My earlier join example, or a for loop? Ok, perhaps you need something like this: my @output = grep /keyword1|keyword2/, $session-cmd('show version'); John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
searching a whole array without using a loop
Hi, Is there a better way to search all elements of an array for a string (without using a loop to test individual elements) than: $line = join(' ',@myarray); if ($line =~ /my string/) { some code } I've seen map and grep used by some but also critisized by some. I thought there might be a function to easily search all elements of an array at once for a string but I can't find one. Cheers Darren -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: searching a whole array without using a loop
On Mon, 23 Aug 2004, Darren Birkett wrote: Is there a better way to search all elements of an array for a string (without using a loop to test individual elements) than: $line = join(' ',@myarray); if ($line =~ /my string/) { some code } I've seen map and grep used by some but also critisized by some. The tools you want for this are probably map and grep. These commands are critisized by some, not entirely unfairly, because code written with them can be hard to understand for people not familiar with the constructs, and you can often get similar functionality out of more verbose (and, to some, ipso facto clearer) code that doesn't use these constructs. But to hell with all that -- if map grep make sense to you, use them. If you are worried about maintainability -- and you should be -- then use comments to explain what you're doing. Commands like these can make your code more compact (and, to some, ipso facto clearer), more efficient, and should be immediately clear to anyone that has put in the effort to become familiar with the language. -- Chris Devers [EMAIL PROTECTED] http://devers.homeip.net:8080/blog/ np: 'Fuel' by Superman from 'Superman - 1940's Radio. eps 01-17' -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: searching a whole array without using a loop
Darren Birkett wrote: Hi, Hello, Is there a better way to search all elements of an array for a string (without using a loop to test individual elements) than: $line = join(' ',@myarray); if ($line =~ /my string/) { some code } I've seen map and grep used by some but also critisized by some. I thought there might be a function to easily search all elements of an array at once for a string but I can't find one. If all the elements are unique then use a hash. if ( exists $hash{ 'my string' } ) { # do something } The most efficient way to determine if an element exists in an array is to use a for loop and exit early: my $found = 0; for ( @array ) { if ( $_ eq 'my string' ) { $found = 1; last; } } If you need to determine the number of matches you can use grep: my $count = grep $_ eq 'my string', @array; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response