Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Error message "Use of uninit"

by bluray (Sexton)
on Nov 21, 2011 at 18:33 UTC ( [id://939289]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I am getting the message "use of uninitialized value $freq in concatenation (.) or string at ... line 15, <$FILE1> line 1". Though, the output is correct, I am having hard time in resolving this error message. I am trying to replace ">" in the first column by "@". My code is below. Any thoughts?

#!usr/bin/perl use strict; use warnings; my $input_file="filetag1.csv"; open my $FILE1, "<", $input_file or die "Cannot open $input_file .$!"; my $output_file="file1tagmod.csv"; open my $FILE2, ">", $output_file or die "Cannot open $output_file .$! +"; while (my $line=<$FILE1>){ chomp $line; my($header,$tag,$freq)=split(' ', $line); $header=~ tr/>/@/d; print $FILE2 "$header\t$tag\t$freq\n"; }

Replies are listed 'Best First'.
Re: Error message "Use of uninit"
by jethro (Monsignor) on Nov 21, 2011 at 18:39 UTC

    You are using split() to split a line into three parts. The error message tells you that $freq has the undefined value, which is what happens if there are not enough parts to split the line into. The error message also tells you that this happens with line 1 (i.e. the first line) of the file you are reading. So presumably line 1 of the file has only one ' ' and therefore only two parts.

    When something like this happens, it is useful to add a line "use Data::Dumper;" to the beginning of your script and a line like "print Dumper(\$line,\$header,\$tag,\$freq);" inside your loop. Start the script and look at the first line. This will help you in debugging even more complicated bugs

      Thanks. You are right. The header line had some aberrant format. The first two entries were separated by "," and the next by ' '; I changed the header and it is solved.
Re: Error message "Use of uninit"
by jdrago999 (Pilgrim) on Nov 21, 2011 at 18:44 UTC

    Also just to clarify - that's a "warning" not an "error" - your program will continue running, but you will see the warning. If you don't want to see warnings about that particular issue (sometimes you know it might be uninitialized but don't care) - you can do this:

    #!/usr/bin/perl -w use strict; use warnings 'all'; ...your code... ..and then... no warnings 'uninitialized'; my ($foo, $bar) = split /\s+/, $some_uninitialized_variable;
Re: Error message "Use of uninit"
by Not_a_Number (Prior) on Nov 21, 2011 at 19:33 UTC

    Since, at least in your posted snippet, you don't use the variables $tag or $freq, perhaps the best way of avoiding the warning would be to change the line

    my($header,$tag,$freq)=split(' ', $line);

    to:

    my ( $header, @rest ) = split ' ', $line;

    Update (thanks AnomalousMonk): And of course you would have to change your output line to:

    print $FILE2 "$header\t@rest\n";
      Hi,

      Thanks for the reply. Though it worked well in resolving the "uninitiated value", the output as I opened in spreadsheet have only 2 columns (2nd and 3rd column got crammed into the 2nd column separated by space).

      Anyway, I resolved the problem by changing the format of the heading line. Header,"Tag" "Frequency" was changed to "Header" "Tag" "Frequency"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 13:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found