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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: Using open in for loop
by Fletch (Bishop) on Jun 14, 2007 at 14:02 UTC

    Had you done use strict and use warnings you'd have gotten helpful diagnostics about leaving off the $ from $i in the for loop. That aside, a more natural way of writing that (presuming you've left out whatever processing you're doing for each file) would have been:

    for my $file ( @array ) { open( FILE, $file ) or die "Can't open '$file': $!\n"; ## Do something with the contents of $file close( FILE ); }
Re: Using open in for loop
by grep (Monsignor) on Jun 14, 2007 at 14:06 UTC
    That is not perl code. It won't even compile.

    I assume you mean something more like this:

    use strict; use warnings; my @array = qw/ foo bar baz blah /; for(my $i = 0; $i < @array; $i++) { print "$i $array[$i]\n"; }
    Which is fine but this is better:
    use strict; use warnings; my @array = qw/ foo bar baz blah /; foreach my $filename (@array) { print "$filename\n"; }

    Please notice I added use strict; and use warnings these are a MUST when you are learning perl.

    Also notice I did not use the actual open - I simplified to find the real root of the problem

    grep
    1)Gain XP 2)??? 3)Profit

A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.