Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Tracing an Uninitialized Error

by ccelt09 (Sexton)
on Jan 30, 2014 at 13:07 UTC ( [id://1072668]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl Monks! I am having difficulty discerning the source of an error in my code and or/input data. I attempt to filter data from one file to another based on what I call start and end positions.

open (CG, "<$cg_input") or die "can't open $cg_input\n"; my @SNPs = <CG>; close(CG); my $interval = "/Users/logancurtis-whitchurch/Desktop/chrX_divisions/" +."$region"."_$filter".".txt"; #specifiecs intervals by region and fil +ter version open (INTERVAL, "<$interval") or die "can't open interval file\n"; foreach ( <INTERVAL> ) { my (undef, $start, $end) = split '\s+', $_; my $switch = 1; while ($switch == 1) { my @get_SNPs = split('\s+', $SNPs[$placeholder]); my $position = $get_SNPs[3]; if (($position < $start) && ($position < $end)) { $placeholder++; } if (($position >= $start) && ($position <= $end)) { print OUT "@get_SNPs\n"; $placeholder++; } if (($position > $start) && ($position > $end)) { $switch =! 1; } } } close(INTERVAL);

The error message returned to me is as follows on repeating loop, until I terminate the program. Line 246 of my interval input file corresponds to the last line of the file, which looks no different from any of the others. I am at a loss regarding the uninitialized value, especially when identically formatted files have worked with the same code.

Use of uninitialized value in split at filter.CGS.pl line 43, <INTERV +AL> line 264. Use of uninitialized value $position in numeric lt (<) at filter.CGS.p +l line 46, <INTERVAL> line 264. Use of uninitialized value $position in numeric lt (<) at filter.CGS.p +l line 46, <INTERVAL> line 264. Use of uninitialized value $position in numeric ge (>=) at filter.CGS. +pl line 50, <INTERVAL> line 264. Use of uninitialized value $position in numeric gt (>) at filter.CGS.p +l line 54, <INTERVAL> line 264. Use of uninitialized value in split at filter.CGS.pl line 43, <INTERVA +L> line 264.

Replies are listed 'Best First'.
Re: Tracing an Uninitialized Error
by toolic (Bishop) on Jan 30, 2014 at 13:16 UTC
      Tip #5 from the Basic outright-wrong-code checklist:
      while ( <INTERVAL> ) {
      It also reveals why 'line 264' seems to be the source of the errors.
Re: Tracing an Uninitialized Error
by Random_Walk (Prior) on Jan 30, 2014 at 15:01 UTC

    Print your ifs...

    ... print "if (($position < $start) && ($position < $end))\n"; if (($position < $start) && ($position < $end)) { ... # etc

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Tracing an Uninitialized Error
by GotToBTru (Prior) on Jan 30, 2014 at 15:20 UTC

    Add the following to the beginning of your code, then run in debug:

     $SIG{__WARN__} = sub { $DB::single = 1 };

    That will stop execution whenever a warning is generated. You can inspect the variables at that point to determine which is undefined.

    Update: added = between } and sub

Re: Tracing an Uninitialized Error
by Crackers2 (Parson) on Jan 30, 2014 at 17:21 UTC

    Most likely explanation is that there's actually an empty line at the end of your data file, which in turn causes the last emelent of @SNPs to be an empty string, which in turn will cause the undefined $position

    Depending on your editor, the empty line might not be easy to spot. Doing a hexdump or similar might help.

      The last two lines of a hexdump looked like this for my input file, not quite an empty line but definitely asymmetrical / an incomplete line at the very end.

      00019a0 30 35 36 30 09 31 35 35 32 37 30 35 36 30 00019ae

      I don't quite know what to make of this, if it is the source of the error or if it can be rectified

        The problem probably is not on line 264. See 1072739.

Log In?
Username:
Password:

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

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

    No recent polls found