in reply to Question about loops and breaking out of them properly
Your next is not inside the grep's block (which is "{ $_ eq $component }"), so it doesn't apply to it, only to the for - and in any case I wouldn't try to affect a grep or map with those control keywords. I would suggest of thinking of map and grep as always applying to the whole list, and it's also best practice for their blocks not to have any side effects.
If you wanted to apply them to only part of the list, I would suggest filtering the list with grep first, or e.g. a slice. For anything more complex, use a for loop.
Another common use case, which might apply in your case, is wanting to know from grep whether there is any match in the list, and stopping the search after that first match, for efficiency. This can be done with e.g. first or any from the core module List::Util. (Update: And apparently Perl is getting builtin any and all.)
In general, you should use Text::CSV / Text::CSV_XS for parsing CSV instead of split, and if I am understanding your code correctly, my node Building Regex Alternations Dynamically might be a technique that's useful to you as well, as regexes could perhaps be faster than a linear search in an array. Update 2: Also Fletch has an excellent point about using a hash in the reply below.
Minor edits for clarity.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Question about loops and breaking out of them properly
by Fletch (Bishop) on Apr 18, 2025 at 13:48 UTC | |
by unmatched (Sexton) on Apr 19, 2025 at 09:54 UTC | |
|
Re^2: Question about loops and breaking out of them properly
by unmatched (Sexton) on Apr 18, 2025 at 12:54 UTC | |
by haukex (Archbishop) on Apr 18, 2025 at 13:47 UTC |