Link to home
Start Free TrialLog in
Avatar of Sandwick

asked on

Why am i getting "Use of uninitialized value $bo_plus in substitution (s///)

this script pick up a file and steps through it, and then picks up another file and matches the item number to merge the file and i getting this error, and I do not know why ?

here is the code where the file is blowing up with the following errors repeatedly.

substr outside of string at ./ line 272, <PLUS> line 745800.
substr outside of string at ./ line 273, <PLUS> line 745800.
Use of uninitialized value $bo_plus in substitution (s///) at ./ line 283, <PLUS> line 745800.
Use of uninitialized value $res_plus in substitution (s///) at ./ line 284, <PLUS> line 745800.
Use of uninitialized value $res_plus in string eq at ./ line 286, <PLUS> line 745800.
Use of uninitialized value $bo_plus in string eq at ./ line 287, <PLUS> line 745800.

while(<PLUS>) {
     $the_plus = $_;
     $item_plus = substr($the_plus, 0,10);
     $bo_plus   = substr($the_plus,36, 5);
     $res_plus  = substr($the_plus,41, 5);

   next if $item_plus =~ m/\s*MSC/i;
     $item_plus =~ s/\s+//;
     next if $item_plus eq "";
   #print " $item  >$item_plus< \t $bo_plus  \t $res_plus \n";
   $bo_plus  =~ s/\s+//; ### Error happens here !!!
   $res_plus =~ s/\s+//; ### Error happens here !!!

         $res_plus = ($res_plus eq "" ) ? '0' : $res_plus;
        $bo_plus = ($bo_plus eq "" ) ? '0' : $bo_plus;
        next if ( $item ne $item_plus);

     if ( $item eq $item_plus ) {
        #$comingSoon = (( $onhand + $transit) - ( $res_plus + $bo_plus ));
        $comingSoon = (($transit) - ( $res_plus + $bo_plus ));
        if ( $comingSoon > 0 ) {
           $csoon = "Available < 35 days (" . $comingSoon .")";
        } else { $csoon = ""; }
        #  print "$comingSoon \t $onhand \t $transit \t $res_plus \t $bo_plus \n";  
Any Help would be appreciated !
thanks in advance
Avatar of wilcoxon
Flag of United States of America image

It's telling you that line 745,800 of your file is less than 36 characters long.  Since the substr use to set $bo_plus and $res_plus are out of range they return undef (so $bo_plus and $res_plus are undef).  Therefore, you get the undef errors when you try to use them in substitutions.
Avatar of Sandwick


$bo_plus and $res_plus are values in a fixed file, that i am picking up and comparing.

is there a way to skip that line, there error must be happening where there is a 0.0 on the line only  ?

See text below

13-5  1 1                11.00    0    0  58  41  12   0.0   1795806216617
13-6  1 1                11.00    0    0  58  41  12   0.0   1795806216624
14-1  1 2                 4.00    0    0  14  14  37   0.0   1795806151734
15-4  1 1   14   10   21 24.00    0    0  25  13  37   0.0   1795806210295
15-6  1 1   15   12   21 20.00    0    0  43  20  21   9.0   1795806210318
15-7  1 1   50   18   50 15.00    0    0  48  34  43   0.0   179580621032
Avatar of tel2
Flag of New Zealand image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is what the file looks like, the substr is there and goes past the 41st position.  See image
Hi Sandwick,

I can't see why you're still getting those errors, sorry.  I can't even reproduce them with the code and data you've provided, even without my "next..." command.  If you want to see the code and data I'm using, let me know.

I suggest you copy your script and cut it down as far as you can while still getting the errors.
Do the same with the data (i.e. cut it down until you just have a couple of lines - maybe have the 1st line work and the 2nd creates the errors).
If the cause is then not obvious to you, post your cut down code and cut down data here, along with the errors you're getting from it.

I assume you are getting those errors because you are using "use strict;" and/or "use warnings;", but I don't see those in your script snippet.
...It's about making a Short, Self Contained, Correct (Compilable), Example:

Often doing this will help you to solve the problem yourself, but it should also make it faster and easier for others to solve it for you.
I will try to isolate it further, The weird this is, the line is longer then the substr, and I am getting the errors.

is there a way to send the line to the screen as I step through the file ? then I can compare what is being imported and what is being picked up ?
Yes there is a way.  Did you read the 2nd paragraph of my first post?
Can you post an attachment of say the 5-10 lines before and after the line with the error?  That way, we can make sure we have the exact format (rather than copy-paste your text post above which may not be exactly the same as your file).
tel2 -> your response to put " next if length($the_plus) < 36;" does work, I put it in the wrong location.

thanks !
OK Sandwick,

Glad to hear it's sorted.

I suggest you use that "Short, Self Contained, Correct (Compilable), Example" method wherever you reasonably can.  I find it often helps me to solve my own problems.

And the other learning point I see from this thread is carefully reading through the posts supplied by experts.  You seem to have misread my first post in 2 areas, i.e. where to insert the "next..." line, and the fact that I had already given you debugging info before you asked for it.  If you take heed of this point, then you could save yourself and experts a lot of time in future.

Are you now saying there are lines which are less than 36 chars long in the input file?  Is line # 745800 one of them?  The ones you showed us didn't seem to be that short, so what's the story?

And if you want to make this:
    $the_plus = $_;
a bit (or even a byte) more concise, you can replace it with:
    $the_plus = $_;
or even:
    chomp($the_plus = $_);

Hi Sandwick,
Thanks for the points, but I would have thought they should have been split over wilcoxon and me.  He did the hard work of working out where the problem was.  I just gave you some code to avoid the issue he'd identified, etc.
I did not know i could split the points, was not that obvious. will pay more attention next time !

thanks for everyones help.
If you want to, I believe you still can (worst case ask an admin to do it for you).