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

I initially discussed this issue in the chatbox but the issue was not easily resolvable there.

The following code is part of a larger loop that iterates through files in a directory and this particular chunk iterates through the lines breaking them in two and assigning the two parts to two variables. The second variable then has the leading white space removed with the regex substitution  s/^\s+//. The problem is that the code gives an 'unitialized value' error.

To clarify the issue I took the offending section and placed it in it's own file and ran just that against a single test file. Same result. So I've verified that the error is in this block of code and the error warning specifically complains about the line: $backlog =~ s/^\s+//;

The code that causes the error is as follows

#!/usr/bin/perl -w use strict; use warnings; use diagnostics; open(REPORT, $ARGV[0]) || die("Error - Can't open $ARGV[0]: $!\n"); our $initial_datetime; our $backlog; while (<REPORT>) { last if /,-/; chomp; ($initial_datetime, $backlog) = split(","); $backlog =~ s/^\s+//; # just for testing purposes print $initial_datetime; print "\n"; print $backlog; print "\n"; }

The file that the code is being run against is this:

11/05/09 12, 3424 11/05/09 13, 3 11/05/09 14, 1 11/05/09 15, 30 11/05/09 16, 73 11/05/09 17, 1 ,-------------------- sum , 3532

The error message is:

Use of uninitialized value in substitution (s///) at qad.pl line 18, < +REPORT> line 1 (#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables. To help...(removed)... Use of uninitialized value in print at qad.pl line 20, <REPORT> line 1 + (#1) Use of uninitialized value in print at qad.pl line 22, <REPORT> line 1 + (#1)

I've initialised the variables (I thought); I've assigned values to them on the split line. This has me completely confused.

Replies are listed 'Best First'.
Re: stripping whitespace gives an 'unitialized value' error
by JavaFan (Canon) on Jun 18, 2009 at 07:52 UTC
    My guess is that the first line of your data file doesn't contain a comma. Perhaps it's a blank line?

      Thank you.

      A preceeding blank line never even registered:  next if /^$/ fixed the issue.

        If your input always follows a particular format, I often find it is useful to write a simple line-parsing sub that returns the correct info (if it exists in the line) and handles any lines that don't conform to the format.
        If you use a regex to match the format rather than splitting, i generally don't find it is that much slower, especially if you precomile the regex (/o modifier), and it gives you the peace of mind that you don't get errors like this.

        sub parse_format{ my $line = shift; my ($date, $number,); ## for returning while ($line =~ m/^ #linestart (\d+\/\d+\/\d+\s+\d+) # capture date and first num +ber \,\s+ #junk in the middle (\d+) # capture the last bit $ # end of string /ox ) { ($date,$number,) = ($1, $2,); } else { warn "\'$line\' did not conform to format. Skipping...\n"; } return ($date, $number,); }

        Or something... I haven't tested this, *just as an example*!!

        Just a something something...
Re: stripping whitespace gives an 'unitialized value' error
by rovf (Priest) on Jun 18, 2009 at 09:20 UTC
    then has the leading white space removed with the regex substitution s/^\s+//

    Laziness is a virtue: Text::Trim.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: stripping whitespace gives an 'unitialized value' error
by andi42 (Initiate) on Jun 18, 2009 at 08:16 UTC
    It seems as if the split didn't return anything useful to the two variables $initial_datetime and $backlog. That would explain, why there are uninitialized values in the lines 20 and 22: The script tries to print uninitialized variables.
    So I guess, too, that there is a line without a comma, so that your split doesn't work.
      I've also added  set number to my .vimrc file to make lines explicit.