Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Data validation and blank spaces in tab formatted csv file

by Tux (Monsignor)
on Oct 24, 2013 at 14:12 UTC ( #1059471=note: print w/ replies, xml ) Need Help??


in reply to Data validation and blank spaces in tab formatted csv file

What keszler said.

my $csv = Text::CSV_XS->new ({ binary => 1, sep_char => "\t", auto_dia +g => 1 }); open my $fh_in, "<", $file_to_parse or die "$file_to_parse: $!"; while (my $row = $csv->getline ($fh_in)) { my ($chu, $dev, $development_name, $bldg_no, $bldg_name, $unit_id, + $proj, $proj_name, $bdsz, $st_no, $street_name, $apt_no, $p_code, $un +_st, $mkt, $max_rent, $fut_movein, $p, $last_move_in, $last_move_out, $pg +m, $clt_no, $mgr_cd, $manager, $unit_status_dt, $cont_occ_dt, $pending_mov +e_out_dt, $unit_status_desc) = @$row;

Enjoy, Have FUN! H.Merijn


Comment on Re: Data validation and blank spaces in tab formatted csv file
Download Code
Re^2: Data validation and blank spaces in tab formatted csv file
by Ma (Novice) on Oct 24, 2013 at 14:31 UTC
    Thank you keszler and Merijin. You have transformed my code to be much better. Can you provide example of one field testing for blanks and empty. For example, if the $unit_status_dt contains data or empty string, put that record into valid file. But if $unit_status_dt contains blank spaces and no data, put that into invalid file. I very much appreciate one example and then I will develop the remaining from there.

      There are several approaches. You can skip those spaces while parsing resulting in empty fields:

      my $csv = Text::CSV_XS->new ({ binary => 1, sep_char => "\t", auto_diag => 1, allow_whitespace => 1, });

      but as you are dealing with tab separated data, I'd personally would coose to do it inside the loop

      while (my $row = $csv->getline ($fh)) { # Check if the 5th field contains data if ($row->[4] =~ m/\S/) { # more than just whitespace $csv_o->print ($fhv, $row); } else { # sorry, this is not filled: invalid $csv_o->print ($fhi, $row); }

      Enjoy, Have FUN! H.Merijn
        Thanks, one issue is still there. If the field is empty (no white spaces) then this is valid because the value was not provided and will go to valid file. But if the field contains only blank spaces, then this is invalid. I just want to remove all white spaces from the field and make it null before doing the comparison. I have tried this:
        chomp($pending_move_out_dt); $pending_move_out_dt =~ s/\s//g;
        But the record is still going to invalid as the field $pending_move_out_dt is empty but containing white spaces. It should contain nothing where there is no data (just null value).
        Thanks, Does Perl distinguish between white space and null. For example, this field contains no data but white space:
        chomp($pending_move_out_dt); $pending_move_out_dt =~ s/\s//g;
        I want to remove all white spaces for that field so that field appear as null (undefined) if there is no data. Any way to do this.
        Thank you. It work fine when the field has value. But when the field contains blank spaces, I am getting this error message: Use of uninitialized value $pending_move_out_dt in concatenation (.) or string at pp6.txt line 52, <FH> line 3. pending_move_out_dt = Use of uninitialized value $pending_move_out_dt in concatenation (.) or string at pp6.txt line 64, <FH> line 3.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (19)
As of 2014-08-22 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls