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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hello monks,

I faced a weird failure while writing a solution to this rosalind problem. I won't paste the whole solution but here is the relevant part:

sub distance { ... do { return $1 + distance($_) if s/\(#:(\d+)\)/#/ } until ... ... }

This code returned a wrong result and a "use of uninitialized value $1 in addition" warning message. Such a warning is weird since I am in a section code where the substitution regex is supposed to have happened, thus affecting $1.

To fix the code, I had to write 0+$1 instead of just $1. (my $x = $1) would have worked too. The warning message disappeared and I ended up with the correct answer. As I understand it, it seems that I had to somehow "nail" the value before using it. But I don't quite understand why.

Any idea?

PS. Here is a simple example suggested below that reproduces the failure (not the warning, though):

sub butter { my $_ = shift; return $1 + butter($_) if s/(\d+)//; return 0; } print butter 'effect / 7ACV06';

It prints 12 with $1, and 13 with 0+$1.


In reply to $1 not "freezing" in an addition by grondilu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (12)
    As of 2014-09-17 17:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (92 votes), past polls