in reply to Re^2: Capturing substrings with complex delimiter, up to a maximum
in thread Capturing substrings with complex delimiter, up to a maximum
The regex will (can only) return valid matches. It cannot return undef. You should not have to use grep.
Thus, it is either your adaption of the code, or your test that is wrong.
The only way you can generate undef's with your first implementation, is if $max is greater than the number of urls within the string, in which case the slice @captures[ 0 .. $max-1 ] will generate undefs.
Instead of greping out the extraneous undef's; don't generate them in the first place:
sub _browseruk_recognize_limited_urls { my ($input, $max) = @_; my @captures = $input =~ m[(https?://.+?)(?:,(?=http)|$)]g; return [ @captures[ 0 .. @captures < $max ? $#captures : $max -1 ] +]; }
But even that is rather silly. You have an array but you want to return an array ref (for some reason?), so you slice the array into a list, wrap it in another (anonymous) array and return a reference to that.
If the reason for returning a reference is "efficiency"; you completely blew any potential gain by splicing and listing (never mind the redundant greping). Better to simply return that list and assign it to an array in the caller.
But, if you really need a reference, then adjust the size of the array you already have and then return a reference to that:
sub _browseruk_recognize_limited_urls { my ($input, $max) = @_; my @captures = $input =~ m[(https?://.+?)(?:,(?=http)|$)]g; $#captures = $max -1 if @captures >= $max; ## Adjust size if necess +ary return \@captures. }
|
|---|