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

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

Replies are listed 'Best First'.
Re: Use of foreach
by Limbic~Region (Chancellor) on Jun 22, 2007 at 15:21 UTC
    Win,
    What happens when you try the following code instead:
    for (@file_names) { print defined($_) ? "$_\n" : "undefined element\n"; } exit;

    Cheers - L~R

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Use of foreach
by guinex (Sexton) on Jun 22, 2007 at 15:29 UTC
    Your @file_names array doesn't contain what you think it does.

    And furthermore, these aren’t the droids you’re looking for.

    Try this:

    use Data::Dumper 'Dumper'; die Dumper \@file_names;
    I expect that you will see an undefined value in your array.

    -guinex

      And that undefined element should be (at least) the first in the array, because the OP only tries to print the first element (and then exits):

      foreach (@file_names){ print $_; exit; }

      citromatik

        I guess the exit; is there to track where something goes wrong at a particular line. I prefer to put die 'OK'; somewhere before/after suspected lines, whenever I'm in hard time debugging code. The program will be instantly death with calming message: OK at __FILE__ line __LINE__.

        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      Hear, hear Win.

      Guinex is absolutely right about using Data::Dumper to view to contents of your trivial (and other not-so-trivial) arrays and hashes.

      This should have been the first thing you thought of when you saw the undefined array value warning. Please write this somewhere near your monitor so you'll have it handy next time it crops up.


      Where do you want *them* to go today?
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Use of foreach
by Fletch (Bishop) on Jun 22, 2007 at 15:29 UTC

    No, it warns in both places.

    $ perl win.pl PRINTING ARRAY Use of uninitialized value in print at win.pl line 7. abc FOREACH Use of uninitialized value in print at win.pl line 11. $ cat win.pl use strict; use warnings; my @array = ( 'a', 'b', undef, 'c' ); print "PRINTING ARRAY\n"; print @array, "\n"; print "FOREACH\n"; foreach( @array ) { print $_; } __END__

    You're not showing the entire picture.

Re: Use of foreach
by syphilis (Archbishop) on Jun 22, 2007 at 15:41 UTC
    Looks like the first value in @file_names is uninitialised. There's nothing wrong with your usage of foreach - except that it's going to exit immediately after printing the first value in @file_names instead of iterating through the entire array. Here's a simple way of generating the warning (and, btw, it is only a warning, not an error):
    C:\_32>perl -wle "@file_names=(undef);foreach(@file_names){print $_}" Use of uninitialized value in print at -e line 1.
    If you're on nix and not windows then you'll need to replace the double quotes with single quotes.

    Cheers,
    Rob
Re: Use of foreach
by blazar (Canon) on Jun 22, 2007 at 16:19 UTC
    I have outlined my problem in the comments of the code. It is a slightly perplexing problem. Am I misunderstanding how the foreach loop works?
    # print @file_names; exit; # This prints out the full array content +s when enabled foreach (@file_names){ print $_; exit; # this gives me the error message: Use of uninitia +lized value in print

    You're either missing some relevant parts of your program, and thus you're doing wrong because we always recommend to prepare minimal but complete examples still exhibiting the problem or you're cheating. with my perl:

    C:\temp>perl -wle "$a[4]=5;print @a" Use of uninitialized value in print at -e line 1. Use of uninitialized value in print at -e line 1. Use of uninitialized value in print at -e line 1. Use of uninitialized value in print at -e line 1. 5

    If the entries of @file_names are all defined first, then for some reason the first one becomes undef, that's a whole 'nother thing, and your problem.

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