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

Re^5: Regex help/ Lua parse

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

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

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

Replies are listed 'Best First'.
Re^6: Regex help/ Lua parse
by marquezc329 (Scribe) on Oct 27, 2012 at 18:52 UTC
    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.

      Another book you may be interested is Perl Best Practices Standards and Styles for Developing Maintainable Code. Amongst all its other good advice, this has an entire chapter devoted to guidelines for code layout.

      Be aware that this book provides guidelines; not rules. Also, as it was released in 2005, it won't make references to features added in more recent versions of Perl (perlhist provides a history of Perl releases, if you're interested).

      -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001216]
[Eily]: I'm tempted to consider sunD for flamebait
[Eily]: while I know that in a void his post doesn't deserve that, we all know the context, and he knows that the technical part of his answer will be ignored
[Eily]: so, with the full context, this is an obvious provocation

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2018-02-22 15:57 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (294 votes). Check out past polls.