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

Here' the problem, brothers:

I have a small script which will (soon) change font size attributes with values of 14 to 16. I declare an array to hold the HTML, open and insert the HTML file into the array and call a wee sub to alter the values.

I'm doing something blatantly, frustratingly wrong and I cannot see it (I think).Should I be declaring and initialising a scalar containing the pattern, and then substitute one for the other.

Here's the code:
#!/usr/bin/perl # change font sizes.plx # Program will read in an html file, change font sizes and print chang +es to DOD command screen. # 1. No need for file variable yet: open (INFILE, "<".$htmlFile) or di +e("Can't read source file!\n"); # 2. /font-size:\s*[0-9]+pt;?/ig use warnings; use diagnostics; use strict; # Declare and initialise variables. my @htmlLines; # Open HTML test file - forward slashes do not need to be escaped. open INFILE, "E:/Documents and Settings/Richard Lamb/My Documents/HTML +/test1InLineCSS.html" or die "Sod! Can't open this file.\n"; # Assign to an array/list variable. @htmlLines = <INFILE>; close (INFILE); changeFontSize(); # Alter attribute values sub changeFontSize { foreach my $line (@htmlLines) { $line =~ s/font-size:\\s*14pt;?/font-size:\\s*18pt;?/ig; # case i +nsensitivity and global search for pattern } } printHTML(); # prints the reformatted HTML file in DOS window sub printHTML { for my $i (0..@htmlLines-1) { print $htmlLines[$i]; } }
Cheers folks,
T

Replies are listed 'Best First'.
Re: More HTML-related regexp probs!
by davorg (Chancellor) on Aug 29, 2003 at 15:30 UTC

    This is the kind of thing that works really well when written as a filter (i.e. a program that reads data from STDIN and writes it to STDOUT).

    #!/usr/bin/perl use strict; use warnings; while (<STDIN>) { s/font-size:\s*14pt/font-size: 18pt/ig; print; }

    But you can also write it on the command line:

    $ perl -i.bak -pe 's/font-size:\s*14pt/font-size: 18pt/ig;' yourfile.h +tml
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: More HTML-related regexp probs!
by tcf22 (Priest) on Aug 29, 2003 at 14:59 UTC
    I don't think you want \\s*.

    This actually matches a single backslash(\), and 0 or more 's'.

    Replace
    $line =~ s/font-size:\\s*14pt;?/font-size:\\s*18pt;?/ig;
    with
    $line =~ s/font-size:\s*14pt;?/font-size: 18pt;/ig;.

    This will match 0 or more whitespace characters in between font-size: 14pt.

    Also replace \\s* in the replacing pattern with a space and remove the ? after the semicolon.