Re: [regexp] Speaking of the /s modifier...



Hans Meier (John Doe) wrote:
Adam W am Dienstag, 7. März 2006 23.16:

Hans Meier (John Doe) wrote:

just to sum up:

$test =~ s{ (.*?) \( (.*?) \) }
{<a href="$2" alt="$2">$1</a>}xsg;

- "\(" instead of "[(]": more readable
- no /m modifier : unnecessary without ^/$-anchors
- /s : may be appropriate for your html source text


The context for this regexp is a simple program I wrote to turn text
files of the form:
someword(http://www.linktosomething.com)
to
<a href="http://www.linktosomething.com";
alt="http://www.linktosomething.com";>someword</a>
(without the linebreaks, of course).
It works by reading in some text file, line by line, using while(<>).


That's ok, so no /s required :-)

Then you can even check if what should be on one line is effectively on one line, by:

$text=~/your_regex_here/x or die "improper line format: $text";

(this dies on empty lines etc. too, and of course the input file's lines should be sanitizes before, and to be on the secure side, the transformation script should do that too... but all that depends on the exact circumstances you use the script)

Sorry, I'm relatively new to programming in general (perl is my first programming language), so I'm not sure what you mean by "sanitizing." Does this imply having the program correct any possible errors in the imput to be compliant with the way the program operates on the input?

Initially, I wanted the program to stick a <p> tag at the beginning of
the file and a </p> tag at the end of the file.

Is it possible to make a regexp that will only match the beginning and ending of a file within
a "while" loop?


Not directly. I can't see at the moment a way to do that without ugly code.

But what should be the sense of that in conjunction with <p>/</p>??


It seems as though you cannot, since "while" only reads in data line by line and thus, even if you remove the newlines from the
input, you can only operate on one line of input at a time. Is this correct?


Not in all cases, see following paragraph.


If it is, then would it be correct to say that, practically speaking, while in a "while" loop, the presence or absence of a /s
modifier will not effect rexep recognition?


In the while (<FILENANDLE>) case, and $/ (perldoc perlvar) set to newline: yes.

No when slurping all lines at once into an array and loop over the array.

Also no if $/ is set to something that reads more than one line.

The $/ variable is very interesting for reading multiline records from a file like conventional lines. Have a look at it!

Thanks for mentioning the '$/' variable, which is probably what I was looking for but didn't know existed (yeah, I know, RTF...)!

I eventually got around this problem by simply printing what I wanted
after opening the file but before the while loop, and then printing
again after the while loop had ended but before the next file is opened.


Sorry, this is not clear to me (what's the purpose of the while in this scenario?)

The while is reading in a file, so it is in the while(<FILEHANDLE>) context. Here is the relevant (paraphrased) code:

print OUT "<p>";
while (defined($lines = <IN>)) {
...make the substitutions to the text...
print OUT $lines;
}
print OUT "</p>";


The p-tags are there because I want each list of links to be an HTML paragraph formatted according to my CSS definition of <p>. Nothing special.

Nice way to post on this list; not the kind of "Oh, I spend 10 secs to ask, there will be people spending 1/2 h to solve my problem".

Thank you. Nothing helps a community more than communication.

Adam


.



Relevant Pages

  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: Puppy Mastiff wants to Nip at Faces
    ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily ... design, PIC code, and real time programming. ...
    (rec.pets.dogs.behavior)
  • Re: Problem with a script
    ... Okay, so variables have unique labels, that doesn't mean they still couldn't be handled in a loop. ... You still have them as uniquely named array indexes... ... I believe that for the new guy this code would be readable, and identifying problems should really not be any more difficult with this, plus I think that it actually might save some time to write the actual code from the beginnig, even though it's not at it's final stage, instead of first writing everything spread out, and then rewriting the same code again cleaned. ... If you expect a person to spend an hour reading your entire code and parsing it in their head, you wont get any help and have to solve the problem by yourself. ...
    (comp.lang.php)
  • Re: Puppy Mastiff wants to Nip at Faces
    ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily access ...   and improve your crack pot licensing fee calculator. ...
    (rec.pets.dogs.behavior)
  • Re: Assitance - Formal Programming
    ... I am presently reading "The Science of Programming" by David Gries, ... Show that P is true before execution of the loop begins. ... >> figure out how I could assertain the weakest precondition? ... > Please note the array limits in the invariant Pi. ...
    (comp.programming)