Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found