Hi John, a few comments on your code.
On Monday 07 Feb 2011 13:50:57 John Delacour wrote: > At 07:11 -0800 05/02/2011, zavierz wrote: > >Here's code which was suggested to me, but when I execute it I'm > >returned to the command line and nothing happens: > > > >#!/usr/bin/perl > >s/^(Article\s+[0-9]+\s+\N*\S)/\\subsection*{$1}/gm > > > >I called this script "Article" and saved it as article.pl > > > > > >The usage then was $perl article.pl oldfile.tex newfile.tex > > What was "suggested to you" is simply a substitution; it can't do > anything unless it has something to do it to. > > Your command gives two arguments (@ARGV, or, if you like, $ARGV[0], > $ARGV[1]) to your script but the script itself makes no reference to > these arguments and they are completely ignored. > > You need to open your in-file (oldfile.tex = $ARGV[0] => $fin) for > reading and open/create your out-file for writing (newfile.tex = > $ARGV[1] => $fout). Each line you read from $fin in the while loop > becomes $_ and you do the substitutions before writing it to $fout. > Actually, see perldoc perlrun - http://perldoc.perl.org/perlrun.html - by giving -p and -i (untested) you can replace the contents of a file "in-place". Now for some comments on your code. > > #!/usr/bin/perl > use strict; Add "use warnings;" too. > my ($fin, $fout) = @ARGV; It's great that you unpack @ARGV like that instead of using $ARGV[0], $ARGV[1] etc. But please say something like $in_fn and $out_fn or something like that. > open FIN, $fin; > open FOUT, ">$fout"; 1. Don't use bareword filehandles. 2. Use three-args-open. 3. Always append or die. See: http://perl-begin.org/tutorials/bad-elements/#open-function-style > while (<FIN>) { To avoid $_ getting tempered with it's a good idea to use an explicit $line variable. > s/^( Article \s+ [0-9]+ .* \S ) > /\\subsection*{$1}/gmx; > print FOUT; > # + if you want to see what you've written > # displayed in terminal/console: > print STDOUT; > } > Regards, Shlomi Fish > # JD -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Escape from GNU Autohell - http://www.shlomifish.org/open- source/anti/autohell/ Chuck Norris can make the statement "This statement is false" a true one. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/