in reply to Read/Create files

Scalar variables need a $ sigil. Use $i, not plain i:
$indexfiles[$i] = $parts[0].'_'.$parts[1].'_'.'INDEX.html'; } else { $indexfiles[$i] = $parts[0].'_'.'index.html';

Replies are listed 'Best First'.
Re^2: Read/Create files
by marinersk (Priest) on Mar 20, 2015 at 20:28 UTC
    You typed faster than me.
Re^2: Read/Create files
by TANYALYNN82 (Initiate) on Mar 25, 2015 at 19:23 UTC
    Thank you all for your help. I added the $ and now it gives this message: Global symbol "@indexfiles" requires explicit package name at index.pl What does this error mean? Thanks!

      Hello TANYALYNN82,

      You must have done more than just add $ to i, or you would be getting different warning messages. My guess is that you also removed this line:

      my @indexfiles;

      which would account for the error message you report. The point to understand is that $indexfiles and @indexfiles are two different variables, the first a scalar, the second an array. Changing one has no affect on the other. Now, when you say:

      $indexfiles[$i] = ...

      you are accessing the element at the $ith index of the array @indexfiles. If you haven’t declared it first (via my @indexfiles) then use strict will generate the error message you are seeing.

      But even when you do declare @indexfiles, the code shown in the OP won’t do what you want, because the scalar variable $indexfiles, which is printed at the end of the foreach loop, is never changed, and so each print will result in a warning like this:

      Use of uninitialized value $indexfiles in print at index.pl line...

      I don’t know what you are trying to do here, so I can’t advise you further; but please read perlintro, especially Perl variable types.

      Two additional points:

      • $parts[0] == 'ABC' attempts to compare whatever is in $parts[0] with the string 'ABC' using the numerical comparison operator ==. You should use eq instead.

      • While not strictly wrong in this instance, having two lexical variables named $build is certainly confusing. It would be better to choose a different name for the foreach variable.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Are you sure it didn't complain about $indexfiles? (Athanasius mentioned it but did not offer a suggestion.)

      This: print $indexfiles, "\n";

      Should be: print $indexfiles[$i], "\n";