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

I am reading in strings from an outside file. Each line has the same number of characters and the same data in the same position within the string. I used the following code to separate the characters into varables:
($beginning,$agency,$district,$ssn,$serv_per_m,$serv_per_y,$serv_per_t +,$last_name,$first_name_i, $middle_name_i,$cover_group, $pay_code, $pay_rate, $earnings, $holder, + $ret_percent,$surv_ben, $work_sch_code,$cont_amt,$cont_code,$stuff) = /(\d)(\d{4})(\d{3})(\d{9})(\d{2})(\d{2})(\d)(\D{10})(\D)(\D)(\d{5})(\d +{2})(\d{8})(\d{6}\D)(\s{8})(\d{4})(\d{3})(\d{3})(\d{5}\D)(\d{2})(.*\s +)$/;
The variables are listed on three lines but the regular expresions are all listed on one long line. I then run a loop to read each line and print several of the variables just to see how it's workiing. It works as desired for several lines then a couple of lines will produce the following error:
Use of uninitialized value $last_name in concatenation (.) or string a +t final_check.pl line 47, <> line 14
Then it will print several lines correctly and then some more of the error messages.

I am currently running PERL 5.10.0 built for MSWin32-x86-multi-thread (with 3 registered patches)

Any suggestions?

Replies are listed 'Best First'.
Re: string error message
by jwkrahn (Abbot) on Jun 15, 2009 at 23:04 UTC

    That probably means that on line 14 in your data file the regular expression does not match the data.

Re: string error message
by markkawika (Monk) on Jun 16, 2009 at 00:52 UTC
    First of all, you can use /x to make that wall of code a lot more readable.

    Secondly, you can test the result of the match to verify that you really got 19 values out:

    my $result = (($beginning, $agency, $district, $ssn, $serv_per_m, $serv_per_y, $serv_per_t, $last_name, $first_name_i, $middle_name_i, $cover_group, $pay_code, $pay_rate, $earnings, $holder, $ret_percent, $surv_ben, $work_sch_code, $cont_amt, $cont_code, $stuff) = / (\d) # $beginning (\d{4}) # $agency (\d{3}) # $district (\d{9}) # $ssn (\d{2}) # $serv_per_m (\d{2}) # $serv_per_y (\d) # $serv_per_t (\D{10}) # $last_name (\D) # $first_name_i (\D) # $middle_name_i (\d{5}) # $cover_group (\d{2}) # $pay_code (\d{8}) # $pay_rate (\d{6}\D) # $earnings (\s{8}) # $holder (\d{4}) # $ret_percent (\d{3}) # $surv_ben (\d{3}) # $work_sch_code (\d{5}\D) # $cont_amt (\d{2}) # $cont_code (.*\s) # $stuff $ /x ); if ($result == 21) { # Test the variables } else { # This line did not match }
      It appears highly likely that you have a non-matching part of the regex that results in an undefined value some of the time. I would use something like code below to debug things. This assigns all of the individual var names into one array (@result) and avoids a mess with specifying each var name.

      Run this on your data set and you see what it matching and what is not. Undefined is a "value" and I don't think that just checking scalar @result is enough. Anyway this should show where the program is failing to match, presumably on line 14 of the input.

      my @result = # note @ variable not $variable !! (($beginning, $agency, $district, $ssn, $serv_per_m, $serv_per_y, $serv_per_t,.............); while (<IN>) { print; my @result = (.....blah) #see previous posters code my $token_nr=0; foreach my $token (@result) { $token_nr++; if (defined ($token) ) #if you try to print an undef { #program will bomb print "$token_nr++\t$token\n"; #this allows you to see exact +ly } #what is undef without progra +m else #bombing { print "$token_nr++\tUNDEFINED\n"; } } }
      I suspect that your regex is too restrictive. Also fixed column files aren't that common but you may indeed have one. (\D) matches exactly one non-digit charater, to allow say somewhere between 1 and 4 non-digit characters, use (\D{1,4}). for a character set, use say  ([\d-]{9,11}) if ssn could be either 123-45-6789 or 123456789. Use \s* to indidicate zero or more space characters.
Re: string error message
by graff (Chancellor) on Jun 16, 2009 at 02:50 UTC
    If your input data really does consist of fixed-width fields, you should probably be looking at unpack, or at least be a little less explicit in your regex. Most of the time, when I've seen "fixed-width field" data, it was normal to see space padding in many of the fields, but your regex doesn't allow for this.

    Anyway, since you have some sort of error-trapping already in place, and you are getting errors that seem to indicate unexpected features in the input data, your next step should be to do some more elaborate checking for data that doesn't match the expected pattern.

    It'll be easier if you create a list of field names, and use a hash with those field names as keys:

    # assuming a row of data in $_, and row number == $. my @fld_names = qw( begin agency district ssn serv_per_m serv_per_y serv_per_t last_name first_name middle_name cover_group pay_code pay_rate earnings holder ret_pcnt surv_ben work_sch cont_amt cont_code stuf +f ); my @fld_types = qw( \d \d+ \d+ \d+ \d+ \d+ \d \D+ \D \D \d+ \d+ \d+ \d+\D \s+ \d+ \d+ \d+ \d+\D \d+ .* ); my @fld_vals = ( /(.)(....)(...)(.{9})(..)(..)(.) (.{10})(.)(.)(.{5})(..)(.{8}) (.{7})(.{8})(....)(...)(...)(.{6})(..)(.*)/x ); # now do some testing of values... for my $i ( 0 .. $#fld_names ) { if ( $fld_vals[$i] !~ /^ $fld_types[$i] $/x ) { warn sprintf( "Data row %d: bad value in field %d (%s): %s\n", $., $i, $fld_names[$i], $fld_vals[$i] ); } } my %fields; @fields{@fld_names} = @fld_vals;
    (I tried to make that match your long list of variables, but you'll want to double-check. It compiles, but is untested. Updated to remove an unwanted "$" from the "fld_types" array.)

    BTW, you need to learn to put <c> ... </c> around code and data when you post -- you can update the OP to add these tags, so that the code is readable.