Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

loop execution

by staeryatz (Monk)
on Oct 25, 2001 at 00:55 UTC ( [id://121270]=perlquestion: print w/replies, xml ) Need Help??

staeryatz has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: loop execution
by mr.nick (Chaplain) on Oct 25, 2001 at 01:12 UTC
    Try this:
    foreach $Line (@code) { $Line =~ s!"(.*)"!<b>"$1"</b>!g; print "$Line\n"; }
    The "g" modifier means to replace globally, not just once (which is the default).

    mr.nick ...

      Thanks!! It worked great. Now I'm off to fix up the rest of my program, and worry about other issues. ;)
Re: loop execution
by jeroenes (Priest) on Oct 25, 2001 at 01:14 UTC
    A quick try:
    my $string = join \000, @code; $string =~ s!("\w+")!<b>$1</b>!g; @newcode = split /\000/, $string;
    See perlre. The regex should be executed each time in the loop BTW. Are you sure that not all the text is in one item for some reason? Try the g modifier after your regex, see if that cures it.

    I also changed the regex a bit. See 7 Stages of Regex Users and Death to Dot Star!. Depending on the complexity of your input, you might be better of with Parse::RecDescent or siblings.

    Jeroen
    "We are not alone"(FZ)

      Yes the g cured it. I've looked over some of the regex things you've directed me to. Thanks. It was a good read...the learning exepeience kinda lost me in the middle, but now I know how powerful regex's in perl can be.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found