I think the major issues are discussed. So I'll just add one important thing that could save you a lot of time in the future. Since you say you are a new programmer - you're lucky to get to know about this early and pick up good habits.

What I'm talking about is coding readably. It might sound unimportant, but believe me, when your programs will start getting longer, even you won't be able to quickly remmember what you wanted to do in lines 20-35 after working for a while on line 315-377 - not to mention different files. If you'll work in a team you'll even cause more casualties.

The first and foremost is to give meaningful names to variables. $i is customarily a loop counter, but if your loop gets longer, nobody will be able to tell that $i holds the file name without some serious reading, sometimes not even you. You don't have to use Java-like names like $fileNameVariableIUseTemporarily :) but it's a lot better to write:

foreach my $param (@params) { ... } foreach my $file (@files) { ... }

instead of

foreach $x (@params) { ... } foreach $i (@files) { ... }

Secondly - indentation. Ok, I can follow your small script here, but what if I get a code with 3-4 nesting levels over 40-50 lines? You'll soon start banging your head against the wall if your indentation will fail you...

Even worse, you sometimes get a block of closing braces - like the last four lines of your script. In really big blocks I comment them so I'll know what ends what (just like good old BASIC where putting the counter name at the end of a FOR loop was standard syntax).

Phew... pretty long. But you'll thank me later :) I had more than my fair share of unreadable code and I'm trying to save you the bother.

And now for something completely different: why

$x=$&; print OUTPUT "$x\n";
when you can print OUTPUT "$&\n"; ?

That's it. Have fun learning programming!


In reply to Re: Using expressions in arrays for pattern matching by yosefm
in thread Using expressions in arrays for pattern matching by Mike_76

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.