Hi SavannahLion?

I'm not sure if this anon Monk is you or somebody else?

You already understand some key fundamentals of NetWallah's code, but you just don't know what you know. An attempt to clarify.

In your code, my ($sb) = $sa =~ m/\((.+?)\)/; You knew that parenthesis were required for ($sb). That puts $sb into a list context. This assigns the first element of the list on the right to $sb. That is the "contents" of the first and only match. If you do not have the paren's, $sb will wind up being "1", the number of matches. I think you figured that part out on your own.

You could have written, my $sb = ($sa =~ m/\((.+?)\)/)[0]; that takes a list slice of the stuff on the right and assigns the zeroth element of that list to the simple scalar $sb on the left. This is functionally the same as your code.

So, my $sab = $sa =~ m/\((.+?)\)/)[0] is just exactly the same as your: my ($sb) = $sa =~ m/\((.+?)\)/;. There is not an explictly named $sb variable, but it is still there, just with some computer generated name that you don't know.

The next part, =~/(\w+)/g is the same as if you had written my (@ab) = $sb =~/(\w+)/g. You used a split instead of the match global regex, my @ab = split(',',$sb); but these statements amount to about the same thing. In NetWallah's version there is not an explictly named array, @ab, but it is there under some computer generated name that you don't know. That array is fed into the join.

So, now I come to my point...

Your code and the more cryptic looking code are about the same and will have similar performance. I actually think your code as written is fine. It takes a few more lines, but it is easy to understand. Giving $sb and @ab explict names doesn't really cost anything. With Perl, fewer lines of code does not necessarily mean "faster" code. I've seen some devilishly tricky lines of code that actually run much slower that a more obvious approach would. My recommendation is to go for clarity as the first priority. Will you or somebody else understand the code a couple of years from now?


In reply to Re^2: Restrict a match within a string by Marshall
in thread Restrict a match within a string by SavannahLion

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.