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

Problem when - use strict and -w

by GertMT (Hermit)
on Jul 14, 2009 at 20:26 UTC ( #780067=perlquestion: print w/replies, xml ) Need Help??

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

The following code that I use to import some csv file works okay if I do not use
#!/usr/bin/perl -w use strict;
However I get warnings ("uninitialized value") if I'm trying to code the right way. Can someone point me further in the right direction?
Thanks for your reply,
#!/usr/bin/perl -w use IO::File; use Text::CSV_XS; my $csv = Text::CSV_XS->new( { sep_char => ',' } ); my $io = IO::File->new( 'data.txt', '<' ) or die "Can't read file: $!\n"; my $number = 10; # which number to start with? until ( $io->eof ) { my $row = $csv->getline($io); print "Olah:","\t", $number++ , "\t", $row->[2], " ", "\"", $row->[5], "\"", "\t", "\"", $row->[3], $row[0], "\"", " ", $row->[4], "\n"; }
with data: data.txt
"123",20090626,14,"P8","Ba",20090626,"BET 98" "123",20090626,74,"P2","Ba",20090626,"BET 08" "123",20090626,47,"03","Av",20090626,"f: 29" "123",20090626,40,"10","Av",20090626,"ok" "123",20090629,17,"07","Go",20090629,"Sa"

Replies are listed 'Best First'.
Re: Problem when - use strict and -w
by moritz (Cardinal) on Jul 14, 2009 at 20:38 UTC
    $row[0], "\"", " ", $row->[4], ^ should be $row->[0]

    That's what use strict; is complaining about when you enable it. Actually $row[0] is an access to @row, about which strict moans.

    Fixing this also makes the warnings go away.

Re: Problem when - use strict and -w
by ikegami (Pope) on Jul 14, 2009 at 21:44 UTC
    Why aren't you using Text::CSV_XS for output too?
    #!/usr/bin/perl -w use strict; use Text::CSV_XS qw( ); my $qfn_in = 'data.txt'; open(my $fh_in, '<', $qfn_in) or die("Can't open file \"$qfn_in\": $!\n"); my $fh_out = \*STDOUT; my $csv_in = Text::CSV_XS->new({ sep_char => ',', eol => $/ }); my $csv_out = Text::CSV_XS->new({ sep_char => "\t", eol => $/ }); my $number = 10; while ( my $row = $csv_in->getline($fh_in) ) { $csv_out->print($fh_out, [ "Olah:", $number++, @$row[2,5,3,0,4], ]); }

    Mind you, the output format is slightly different than yours. (It will quote all fields or only necessary fields depending on option always_quote).

    It could use error checking, but you didn't have any either.

      Like your proposed code of reading the data. The empty last line problem is now also not an issue anymore. Don't think I can have Text::CSV_XS handle the output as I need to work on the output such as: (psuedocode)
      multiply row->[2] by 4 if row->[0] != 123
      Though definitely something I'll consider using in the future.
        What part of that do you think you can't do with $csv->print (but can somehow do with STDOUT->print)
Re: Problem when - use strict and -w
by toolic (Bishop) on Jul 14, 2009 at 20:42 UTC
    When I run your code without -w or use strict;, I get no complaints. However, when I run it with -w, I get your "uninitialized value" warnings, and I get the following warning:
    Name "main::row" used only once: possible typo at ...

    From this line of code:

    $row[0], "\"", " ", $row->[4], ^^^^

    Did you see this warning too?

    You should fix that error and continue to use strict and warnings.

      Thank you both for your prompt reply.
      I had been staring at the code for a while but didn't see the typo in my code. Seemed that in the real code an empty last line of the data.txt also gave some problem. I'll find my way out of that (tomorrow, as it's too late here by now)
      And yes, I'll continue to use strict and warnings!
      Thanks again.
        As an aside, you could reduce the amount of typing by using qq in your print:
        print "Olah:\t", $number++, qq(\t$row->[2] "$row->[5]"\t"$row->[3]$row->[0]" $row->[4]\n);

        I'm not sure if it's any prettier, but it is easier to type than all those commas and quotes and back-whacked quotes.

Re: Problem when - use strict and -w
by roboticus (Chancellor) on Jul 15, 2009 at 04:05 UTC

    If you can't see what perl is complaining about, it's sometimes helpful to add a "use diagnostics;" statement, so it'll tell you more detail and/or some suggested things to look for. I used it quite a bit when I was starting out with perl.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://780067]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-12-08 06:50 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (34 votes). Check out past polls.