Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^3: Regex help/ Lua parse

by kcott (Chancellor)
on Oct 27, 2012 at 16:16 UTC ( #1001209=note: print w/replies, xml ) Need Help??

in reply to Re^2: Regex help/ Lua parse
in thread Regex help/ Lua parse

Firstly, you've done nothing wrong in replying twice to the same node. You've done this in a perfectly acceptable fashion: asking different questions which stem from the same previous response. Here's an example of me doing the exactly same thing in last 24 hours: Perl/TK borderwidth question - note the two Re^3: Perl/TK borderwidth question responses. Updating your node (and perhaps sending a /msg indicating such updating) is the more usual way; however, in this instance, you've done the right thing: thunderbolts from the gods may prove me wrong. *gulp* :-)

My personal preference for links to books is that they target the publisher not some arbitrary vendor. A vendor will not advertise books that they do not have in stock. Mastering Regular Expressions is published by O'Reilley and this company, in particular Tim O'Reilley, has been a particularly good friend to Perl over the years - the company your posted link refers to shows no such affiliation. The link I would have provided for this book is: Mastering Regular Expressions (i.e. actual markup: [|Mastering Regular Expressions] [I do have a copy of that book myself and - yes - it's wonderful! :-)]

OK - Off the soap box and back to the code.

Wherever you have non-capturing parentheses containing alternation (e.g (?:x|y|z)), it's usually better to avoid backtracking with the (?>...) construct (e.g. (?>x|y|z)). See perlre - Backtracking, perlre - Extended Patterns and pp. 102-107 in Friedl's book (check the index - page numbers may differ in your version).

You say: "As a result, the regexps in this code are rather long, but I hope still readable.". There's absolutely no reason for them to be unreadable due to length: just use the x option. Furthermore, for those variables not dependent on a loop variable, you can compile them outside of the loop once. Here's an example:

sub some_function { my $re = qr{ \A # start of string (?> x | y | z ) # match exactly one of x, y or z \z # end of string (ignore optional terminal new +line) }x; while (<>) { if (/$re/} { # do something based on successful match } } }

Finally, I am absolutely not going to tell you to adopt any particular coding style; however, I am going to urge you to adopt a coding style that's easy for you and others to read. Have a look around the Monastery, see how other Monks write their code, then pick something you're comfortable with. Beyong the indentation issues, the code you presented at the start of this thread was superior to what you now have. If you seriously don't understand what you read in perlstyle, then please ask for clarification.

-- Ken

Replies are listed 'Best First'.
Re^4: Regex help/ Lua parse
by marquezc329 (Scribe) on Oct 27, 2012 at 16:54 UTC

    Thank you for your response. Updated the reference to Mastering Regular Expressions above. Can you please explain how the previous code was superior? I see the indentation problems that you are referring to, and I tried to be sure to follow the guidelines specified in perlstyle, i.e. 4 space indentations, curly brackets on same line or lined up vertically, space around most operators. Should I have used

     $theme_table{$1} = $2 if (/regex/); instead of  if (/regex/) { $theme_table{$1}


    foreach my $table (@_) { foreach my $key (sort keys %$table) { print "$key => $table->{$key}\n" } print "\n"; }
    instead of
    foreach my $table (@_) { foreach my $key (sort keys %$table) { print " $key => $tabl +e->{$key}\n" } print "\n"; }

    If you could clarify exactly where it is that I'm missing the mark stylistically it would be greatly appreciated. I think good style is the most difficult thing to learn out of a book. Thanks again for your help.

      Compare your original version (with all regexes reduced to regex to alleviate noise)

      sub buildTables { while (<>) { if (/regex/) { $theme_table{$1} = $2 } if (/regex/) { $taglist_table{$1} = $2 } if (/regex/) { $menu_table{$1} = $2 } } #print Dumper \%theme_table; #print Dumper \%taglist_table; #print Dumper \%menu_table; }

      with this alternative version

      sub buildTables { while (<>) { if (/regex/) { $theme_table{$1} = $2 } if (/regex/) { $taglist_table{$1} = $2 } if (/regex/) { $menu_table{$1} = $2 } } }

      Which looks clearer to you? Is the closing brace of the while loop more or less obvious to you now? How about providing me with some examples of code that looks like yours.

      -- Ken

        Aaah. I see. It seems much more obvious to me with the reduced regexes. I've never used such extensive regexes and I think I may have been letting myself get overwhelmed and distracted by the length and complexity of these statements in comparison to the shorter more basic code I've been learning with. So in essence,
        if (x = y) { print x; }
        and, in simple situations
        if (x = y) { print x; }
        are ok, but
        if (x = y) { print x; }
        is not. In this stripped down form I can see how the last example, although interpreted properly by Perl, can look like a random  if (x = y) and an unrelated block of { print x; }. I'm not entirely sure of what kind of examples you are asking for, but from this node:
        if ($Age < 13) && ($Age > 0)) { print "What are you doing here, $firstName? This Web site is strictly PG-13. Shame on you for being so naughty as to come here!"; } elsif ($Age < 18) && ($Age > 12)) { print "Not to long ago, we’d have have chased you away from our Web site. But now that you’re a teenager and mature enough for PG-13 materials, we’re glad to see you $firstName."; }
        has the same qualities as my code and could be cleaned up (focusing on the indentation and curly braces) by saying:
        if (($Age < 13) && ($Age > 0)) { print "content"; } elsif (($Age < 18) && ($Age > 12)) { print "content"; }
        or even:
        if (($Age < 13) && ($Age > 0)) { print "content"; } elsif (($Age < 18) && ($Age > 12)) { print "content"; }
        Thank you again for taking time out to help me grasp the fundamentals. Please let me know if I am missing your point. I enjoy Perl (and learning programming in general) and feel I have been progressing rather quickly, but I want to squash bad habits and form a good foundation in style and understanding before moving to some of the more complex concepts and modules in Perl.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001209]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2017-05-24 14:24 GMT
Find Nodes?
    Voting Booth?