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

Hi folks,

Many thanks for the help recently. I have another query for the Wise. Here's a brief explanation of my code, which you'll find below. I'm opening an HTML file on my hard-drive, slurping it into an array (if jeffa's out there, I've adopted the approach as suggested, but I'm just playing with this for the time being - going to a similar control loop later), then testing for the presence (or lack) of word spacing attributes within in-line styles.

If the pattern doesn't match, the attribute is added to certain in-line styles (header, paragraph and list tags). The document is then printed on-screen. Otherwise, a warning is printed instead

The code seems to work, but an error message appears. Here goes:

Use of unitialized value in array element at controlflow.plx (#1) line 21 (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a + mistake. To suppress this warning assign a defined value to your var +iables.

I'm doing something very obvious, bu cannot see it, aint I? Here's the code:

#!/usr/bin/perl # controlflow.plx =head1 DESCRIPTION Program will read in an html file and add word spacing style attribute +s to header, paragraph and list tags. and print out doc in DOS window. Presence of tags/attributes will be t +ested for via an 'if-else' control structure. =head2 ALTERNATIVE FILE OPENING CODE $htmlFile = "E:/Documents and Settings/Richard Lamb/My Documents/HTML/ +dummy.html"; open (INFILE, "<".$htmlFile) or die("$!: Can't read source file!\n"); =cut use warnings; use diagnostics; use strict; # Declare and initialise variables. my @htmlLines; # Open HTML test file and read into array. open (INFILE, "E:/Documents and Settings/Richard Lamb/My Documents/HTM +L/dummy.html"), or die ("$!: Can't open this file.\n"); @htmlLines = <INFILE>; close (INFILE); # Check for attribute pattern in file if($htmlLines[my $i] !~ m/word-spacing:\s?[\d]+px/ig) { # Loop through file stored in array, inserting attribute foreach my $line (@htmlLines) { $line =~ s/(<h[1-6]\sstyle=.*)">/$1; word-spacing: 30px">/ig; # c +ase insensitivity and global search for pattern. $line =~ s/(<p\sstyle=.*)">/$1; word-spacing: 10px">/ig; $line =~ s/(<li\sstyle=.*)">/$1; word-spacing: 10px">/ig; } # Print modified file in DOS window for my $i (0..@htmlLines-1) { print $htmlLines[$i]; } } else { print "Attribute not in HTML file\n"; }

Replies are listed 'Best First'.
Re: Control flow question
by Abigail-II (Bishop) on Sep 05, 2003 at 12:14 UTC
    I'm doing something very obvious, bu cannot see it, aint I? Here's the code:

    You know, if you get a warning or an error message with a line number, and ask about it, don't just post a blob of code, also indicate which line the warning is about. Now you've already gotten two replies, and both replies guess a different line where the error might be. At least one of the answers is about the wrong line.

    Number your lines, or place a comment in the code "this is line 21". A little courtesy towards the people answering is welcome.

    Abigail

      My apologies. Thanks for tip. The modified code will be sent forthwith. Regards Richard
Re: Control flow question
by Aragorn (Curate) on Sep 05, 2003 at 11:59 UTC
    I don't think you can declare (with my) a variable in an array lookup. I think you messed up the looping through the array with HTML lines. In the
    if($htmlLines[my $i] !~ m/word-spacing:\s?[\d]+px/ig)
    line, what value do you expect $i to have?

    Arjen

Re: Control flow question
by liz (Monsignor) on Sep 05, 2003 at 11:53 UTC
    I think you want to lose the comma in:
    open (INFILE, "E:/Documents and Settings/Richard Lamb/My Documents/HTM +L/dummy.html") or die ("$!: Can't open this file.\n"); + ^

    Liz

Re: Control flow question
by CountZero (Bishop) on Sep 05, 2003 at 13:05 UTC

    Your code works without errors or warnings on my Activestate Perl 5.6.1 with all warnings enabled.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law