merlyn's code doesn't really do the same thing as mkmcconn's code. He checks for a good $base or a good $ext.

mkmcconn's code is actually looking for a good $base explicitly. Assuming this is the intended behavior, the code should look like this:

# mkmcconn meaning here: while (<DATA>) { # not "for"... no need to suck all into memory (my ($base, $ext) = /(\w+)\.(tiff?)\b/) if ($base) { # we have a good $base } } # merlyn style here while (<DATA>) { # not "for"... no need to suck all into memory if (my ($base, $ext) = /(\w+)\.(tiff?)\b/) { # we have a good $base or $ext } }

It is possible to squeeze the check for $base's validity into the same line with the declaration and assignment, but I couldn't think of a way to do it that wasn't ugly. Perhaps someone more skilled than I can think of a way.

Thanks to mkmcconn for pointing out the cool way to use regexes, and to merlyn for the elegant restatement of the script.

Update TGI removes foot from mouth and attempts to put it right back in. If I am not completely confused the distinction would need to be made if either parenthetical in the regex could match a null value. Like (\d*). If you do get a null match, are the $\d variable assignments unaffected? How about assigning into a list, are the undefined values dropped? Not that I thought of this before I posted, I didn't get that the match value had to be 0 or 2.

Update 2 I didn't have time to check the questions I asked yesterday, but I did this morning. Lookout below and you'll get to see what happens with NULL behavior.

@strings=('aaaaaa','11111aaaaa'); foreach (@strings) { my $ord = (my ($number, $letter) = /(\d*)([a-zA-Z]+)/); print "$_\n"; print "\tORD = $ord\tNUM = $number\tLET = $letter\n"; }

Results:

aaaaaa
        ORD = 2 NUM =   LET = aaaaaa
11111aaaaa
        ORD = 2 NUM = 11111     LET = aaaaa

TGI says moo

In reply to Re: Re: regex for assignment by TGI
in thread regex for assignment by mkmcconn

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.