Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Use of uninitialized value $data

by ImJustAFriend (Scribe)
on Nov 13, 2018 at 21:17 UTC ( [id://1225750]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, I am in need of assistance please. I have a file I am parsing. At the end of parsing, I am left with lines that look like this:
label1=data1,label2=data2, ...
There can be a variable number of label/data pairs on each line. My code for parsing these lines:
my @data = split(',', $line); foreach my $datapair (@data) { my ($label, $data) = split('=', $datapair); print "label: $label; data: $data\n"; print OUT1 "$data;"; }
When I run the code, I get on my stderr
Use of uninitialized value $data in concatenation (.) or string at ./t +est.pl line 124, <IN4> chunk 2147. Use of uninitialized value $data in concatenation (.) or string at ./t +est.pl line 125, <IN4> chunk 2147.
I can see in my debug logs (further up, code not shown) that I am getting @data loaded appropriately with lines like my sample lines above. So why is $data uninitialized? This has been driving me nuts for a couple of hours now. Thanks all!

Replies are listed 'Best First'.
Re: Use of uninitialized value $data
by Corion (Patriarch) on Nov 13, 2018 at 21:23 UTC

    My hint is to not trust your logging further up and to move the logging to where the problem is. If $data is undef, then $datapair does not contain a =.

    So, investigate $datapair and the contents of @data just before your loop.

    Maybe/most likely, $line is not what you think it is. Maybe it ends with a comma and a newline, and split helpfully keeps that newline as its own element in @data?

      Thanks for the input! I just ran it again. Here is what $line (one example) looks like in my debug log (obscured for privacy):
      "line: label1=N,label2=N,label3=N,label4=NNNNNNNNNNN,label5=N,label6=W +WWN"
      So it looks as though $line is populating properly... hmmmm....

        Then please show us the relevant code+data that produces the output.

        I can't reproduce the problem using the following, very short program:

        #!perl use strict; use warnings; my $line = "line: label1=N,label2=N,label3=N,label4=NNNNNNNNNNN,label5 +=N,label6=WWWN"; my @data = split(',', $line); foreach my $datapair (@data) { my ($label, $data) = split('=', $datapair); print "label: $label; data: $data\n"; #print OUT1 "$data;"; } __END__ label: line: label1; data: N label: label2; data: N label: label3; data: N label: label4; data: NNNNNNNNNNN label: label5; data: N label: label6; data: WWWN

        ... to me, that means, you are either doing something differently in your code that you haven't shown, or your data is something else.

        Perhaps use a hash?
        #!/usr/bin/perl use strict; use warnings; my $line ="line: label1=N,label2=N,label3=N,label4=NNNNNNNNNNN,label5= +N,label6=WWWN"; my %values = $line =~ /(\w+)=(\w+)/g; foreach my $label (sort keys %values) { print "$label $values{$label}\n"; } __END__ label1 N label2 N label3 N label4 NNNNNNNNNNN label5 N label6 WWWN
Re: Use of uninitialized value $data
by LanX (Saint) on Nov 13, 2018 at 21:46 UTC
    Did you have a look at chunk 2147?

    Why don't you print $line if not defined $data; ?

    Even better use Data::Dumper to check for unexpected characters.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Use of uninitialized value $data
by Marshall (Canon) on Nov 13, 2018 at 21:48 UTC
    Look at line 2147 of the data.
    This could be a blank line?

    Perhaps a regex is better?

    #!/usr/bin/perl use strict; use warnings; $|=1; #turn of buffering for STDERR while (my $line = <DATA>) { next unless $line =~ /\S/; #skip blank lines my ($label, $data) = $line =~ /\s*(\w+)\s*=\s*(\w+)/; die "bad line: $line" unless defined $data; print "label $label is: $data\n"; } =prints label a is: 3 label b is: 4 label c is: 55 label d is: 99 bad line: x= Process completed with exit code 255 =cut __DATA__ a = 3 b=4 c = 55 d=99 x=
Re: Use of uninitialized value $data
by haj (Vicar) on Nov 13, 2018 at 21:48 UTC

    I was about to make the same suggestions as Corion did, only I am typing slower. Here's one more possible input: If you have an empty input line, i.e. a line which contains only a newline "\n", you get the same warnings.

    And, of course, you should closely inspect chunk 2147 of your input file.

Re: Use of uninitialized value $data
by hippo (Bishop) on Nov 14, 2018 at 09:28 UTC

    As pretty much everyone else has said now the problem is in your data - the stuff you are not showing us. See How to ask better questions using Test::More and sample data for rationale and instructions to produce an SSCCE like this one.

    use strict; use warnings; use Test::More tests => 1; use Test::NoWarnings; my $line = 'foo=a,bar=3,baz=Fizzlegrapplebaum'; my @data = split(',', $line); foreach my $datapair (@data) { my ($label, $data) = split('=', $datapair); my $out = "label: $label; data: $data\n"; }

    Of course, here the test succeeds because the initial data is good. Your task is to make this test (or one very similar to it) fail by supplying the duff value for $line.

Re: Use of uninitialized value $data
by ImJustAFriend (Scribe) on Nov 13, 2018 at 22:00 UTC
    Thanks for all the thoughts on the chunk. Unfortunately I'm getting this pair of errors for every input chunk in the input file... :(
      Run my code and see what it does.
      Try this as an alternative:
      foreach my $datapair (@data) { my ($label, $data) = split('=', $datapair); ###### die "Bad Line: $datapair" if !defined ($data); ##### print "label: $label; data: $data\n"; print OUT1 "$data;"; }
      Then please show the content of the @data array using Data::Dumper.
      please show a few lines of the input file that demonstrates the problem.
Re: Use of uninitialized value $data
by ImJustAFriend (Scribe) on Nov 15, 2018 at 21:48 UTC
    Thank you everyone. I found out today they had me looking at the wrong log file, so this code is no longer being used...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-25 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found