Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: string error message

by markkawika (Monk)
on Jun 16, 2009 at 00:52 UTC ( [id://771841]=note: print w/replies, xml ) Need Help??


in reply to string error message

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 }

Replies are listed 'Best First'.
Re^2: string error message
by Marshall (Canon) on Jun 16, 2009 at 04:25 UTC
    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.
        Not necessarily. That may not happen in the general case. This gets complex. In this particular regex, I think you are right, the first undef results in subsequent undef's. In any event the code I showed will show what happens. As a response I tried to show a method to debug this. The "here is how to grow wheat" instead of "here is some wheat" idea. I'm hoping that it helped.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://771841]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-09-09 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.