Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: regex for assignment

by merlyn (Sage)
on Feb 27, 2001 at 03:26 UTC ( [id://60976]=note: print w/replies, xml ) Need Help??


in reply to regex for assignment

Another way to write this that I find more natural than your "if defined" above is:
while (<DATA>) { # not "for"... no need to suck all into memory if (my ($base, $ext) = /(\w+)\.(tiff?)\b/) { # we have a good $base and $ext } }
I'm using a list assignment in a scalar context (the boolean for the if). If the match fails, it's 0 elements, thus false. If the match succeeds, one or more element, thus true.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: regex for assignment
by TGI (Parson) on Feb 27, 2001 at 04:28 UTC

    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
      merlyn's code uses the feature of list assignment in scalar context. This returns the number of elements being returned, which, in this case, is either 0 or 2. If the regex matches, then it returns $1 and $2, which means that $base and $ext get their values.

      If the regex matches, then the two variables have values -- one can not be defined if the other isn't. Thus, merlyn's code does the right thing.

      japhy -- Perl and Regex Hacker

      Actually, it's not "or", it's "and". I'm checking for both a good $base and $ext implicitly, or rather, I don't need to check. If the regex matches, both $base and $ext will be set properly. If the regex fails, the match list length is 0, and the if fails.

      -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://60976]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found