I seek enlightenment, my Perl brothers...
I'm re-writing a program of mine, originally made in C++,
thinking the best way to learn Perl is to write programs
with it, and see how it differs from the other languages.
My
program parses source
code to generate HTML files with highlighting. I'm trying
to make a CGI version of it now.
Here's my problem: I'm running a foreach loop for an array,
and the block of code inside the loop contains regex's. Each
of the regex's only get executed
once for the whole
array, instead of being executed for each array cell. I've
tried just a regular for loop, and I still get the same thing.
Here's a similar code snippet:
foreach $Line (@code) {
$Line =~ s!"(.*)"!<b>"$1"</b>!;
print "$Line\n";
}
If the @code array contained (each line being an array cell):
this "that" this
"and" that "and"
this "and" that
The output would be:
this
"that" this
"and" that "and"
this "and" that
But, what the output
should be (or what I'm hoping for) is:
this
"that" this
"and" that
"and"
this
"and" that
Can somebody show me why the regex only gets executed once,
(for only the first appearance of the expression), and not
for every time through the loop? Any help would be greatly
appreciated.
BTW: I'm using Perl 5.6.1 on a Linux box, and also: Perl is
a very sexy language. ;)
Edit Masem - Code Tags
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.