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

Dear Monks,

Why does the following bit of code:

foreach (@CHOICE) { if ($_ =~ /2001\sCensus\sOutput\sArea/){ #<< error message applie +s to here
give me:
Use of uninitialized value in pattern match m//) :/Directory/File_name +.pl line 246, <FLAT_FILE> line 31.
Does it mean that one of the @CHOICE array contents are empty?

Replies are listed 'Best First'.
Re: An error message to understand
by tirwhan (Abbot) on Jan 06, 2006 at 12:09 UTC
    Does it mean that one of the @CHOICE array contents are empty?

    Yes. You can avoid that warning by checking for definedness:

    foreach (@CHOICE) { if (defined && m/2001\sCensus\sOutput\sArea/){
    or
    foreach (@CHOICE) { next if (!defined); if (m/2001\sCensus\sOutput\sArea/){

    This uses $_ implicitly in both the definedness test and the pattern match. Often it's clearer to assign the value to a lexical variable:

    for my $chosen (@CHOICE) { if (defined $chosen && $chosen =~ m/2001\sCensus\sOutput\sArea/){

    As a further comment, usage of upper-case variable names is generally reserved for constants or perl private variables.


    There are ten types of people: those that understand binary and those that don't.
      And since the pattern can never match the empty string, or 0, you could skip the define and write:
      foreach (@CHOICE) { if ($_ && /.../) { ... } }
      Or:
      foreach (@CHOICE) { $_ or next; if (/.../) { ... } }
      if all you have inside the foreach is the if statement.
      Perl --((8:>*

        I don't particularly want to argue about it but I would not approve of such a change. True, as the pattern match is now it does not make a difference, but if the pattern is ever changed it's easy to miss changing the test as well, which can introduce a subtle bug. Just checking for truth makes the code more brittle. IMO, YMMV, etc.


        There are ten types of people: those that understand binary and those that don't.
Re: An error message to understand
by Fang (Pilgrim) on Jan 06, 2006 at 11:53 UTC

    If the @CHOICE array was empty, the loop would never get executed. However, it is very likely that (at least) one of the elements of @CHOICE is undefined. Maybe you should show us a little bit more of your code.

Re: An error message to understand
by kulls (Hermit) on Jan 06, 2006 at 12:05 UTC
      I use the supper search every night. Last night it found me some tasty Chinese food.
Re: An error message to understand
by TomDLux (Vicar) on Jan 06, 2006 at 17:58 UTC

    Asking the Wise Monks(reg) to explain odd aspects of perl is generally a Wise Move(reg), but I wonder if it's the most time-efficient way of answering your question.

    I would have suggested altering the script to place a print statement or two at that point:

    foreach (@CHOICE) { print "\$_ is ", ( defined $_ ? "'$_'.\n" : "undefined.\n" ); if ($_ =~ /2001\sCensus\sOutput\sArea/){

    Or ( my preference ) run the program in the debugger, and find out what's happening at that point.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA