Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Read/Create files

by TANYALYNN82 (Initiate)
on Mar 20, 2015 at 20:11 UTC ( [id://1120800]=perlquestion: print w/replies, xml ) Need Help??

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

I am a beginning perl programmer. I need help with these errors I'm seeing. The errors are as follows: Argument "i" isn't numeric in array element at projects.pl line 27. Bareword "i" not allowed while "strict subs" in use at projects.pl line 23. I want to read specific files from a directory and store the file names in an array. I want to generate html files using part of the names taken from the array excluding certain files in the directory. Here is my script. Thanks, in advance!

use strict; use warnings; my $build = 'C:\mydir'; opendir DIR, $build or die "cannot open directory $build: $!"; my @allprojects = grep { $_ ne '.' && $_ ne '..'} readdir DIR; my @indexfiles; my @parts; my $i = 0; my $indexfiles; foreach my $build (@allprojects) { @parts = split(/_/, $build); if ($parts[0] == 'ABC') { $indexfiles[i] = $parts[0].'_'.$parts[1].'_'.'INDEX.html'; } else { $indexfiles[i] = $parts[0].'_'.'index.html'; } ++$i; print $indexfiles, "\n"; } closedir DIR;

Replies are listed 'Best First'.
Re: Read/Create files
by toolic (Bishop) on Mar 20, 2015 at 20:27 UTC
    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';
      You typed faster than me.
      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";

Re: Read/Create files
by marinersk (Priest) on Mar 20, 2015 at 20:28 UTC

    i is not numeric.

    $i is.

    Welcome to Perl. :-)

      Luckily, the OP used the strictures, and it resulted in compile errors. Without them, some code will compile and silently do the wrong thing:
      perl -E'@n=7..9;$x=$n[i];say$x' 7

      In this case, i is kinda evaluated as numeric :)

      perl -MO=Deparse -E'@n=7..9;$x=$n[i];say$x' @n = 7..9; $x = $n[0]; say $x; -e syntax OK

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1120800]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-18 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found