Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE:</strong></p> <p>Based on the results of discussion in <a href="https://stackoverflow.com/questions/3019925/is-map-a-loop">this question</a>, depending on your intent/criteria of what constitutes "not using a loop", the <code>map</code> based solution below (see "<strong>Option #1</strong>) may be the most concise solution, provided that you don't consider <code>map</code> a loop (the short version of the answers is: it's a loop as far as implementation/performance, it's not a loop from language theoretical point of view). </p> <hr> <p><strong>Assuming you don't care whether you get "3" or "Sony" as the answer</strong>, you can do it without a loop in a simple case, by building a regular expression with "or" logic (<code>|</code>) from the array, like this:</p> <pre><code>my @strings = ("Canon", "HP", "Sony"); my $search_in = "Sony's Cyber-shot DSC-S600"; my $combined_search = join("|",@strings); my @which_found = ($search_in =~ /($combined_search)/); print "$which_found[0]\n"; </code></pre> <p>Result from my test run: <code>Sony</code></p> <p>The regular expression will (once the variable <code>$combined_search</code> is interpolated by Perl) take the form <code>/(Canon|HP|Sony)/</code> which is what you want. </p> <p>This will NOT work as-is if any of the strings contain regex special characters (such as <code>|</code> or <code>)</code> ) - in that case you need to escape them</p> <p><strong>NOTE</strong>: I personally consider this somewhat cheating, because in order to implement <code>join()</code>, Perl itself must do a loop somewhere inside the interpeter. So this answer may not satisfy your desire to remain loop-less, depending on whether you wanted to avoid a loop for performance considerations, of to have cleaner or shorter code.</p> <hr> <p>P.S. To get "3" instead of "Sony", you will have to use a loop - either in an obvious way, by doing 1 match in a loop underneath it all; or by using a library that saves you from writing the loop yourself but will have a loop underneath the call.</p> <p>I will provide 3 alternative solutions.</p> <p><strong>#1 option:</strong> - my favorite. Uses "map", which I personally still consider a loop:</p> <pre><code>my @strings = ("Canon", "HP", "Sony"); my $search_in = "Sony's Cyber-shot DSC-S600"; my $combined_search = join("|",@strings); my @which_found = ($search_in =~ /($combined_search)/); print "$which_found[0]\n"; die "Not found" unless @which_found; my $strings_index = 0; my %strings_indexes = map {$_ =&gt; $strings_index++} @strings; my $index = 1 + $strings_indexes{ $which_found[0] }; # Need to add 1 since arrays in Perl are zero-index-started and you want "3" </code></pre> <p><strong>#2 option</strong>: Uses a loop hidden behind a nice CPAN library method:</p> <pre><code>use List::MoreUtils qw(firstidx); my @strings = ("Canon", "HP", "Sony"); my $search_in = "Sony's Cyber-shot DSC-S600"; my $combined_search = join("|",@strings); my @which_found = ($search_in =~ /($combined_search)/); die "Not Found!"; unless @which_found; print "$which_found[0]\n"; my $index_of_found = 1 + firstidx { $_ eq $which_found[0] } @strings; # Need to add 1 since arrays in Perl are zero-index-started and you want "3" </code></pre> <p><strong>#3 option:</strong> Here's the obvious loop way:</p> <pre><code>my $found_index = -1; my @strings = ("Canon", "HP", "Sony"); my $search_in = "Sony's Cyber-shot DSC-S600"; foreach my $index (0..$#strings) { next if $search_in !~ /$strings[$index]/; $found_index = $index; last; # quit the loop early, which is why I didn't use "map" here } # Check $found_index against -1; and if you want "3" instead of "2" add 1. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload