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

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: Capturing substrings with complex delimiter, up to a maximum by BrowserUk
in thread Capturing substrings with complex delimiter, up to a maximum by jkeenan1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.