The code (running under Perl 5, version 26, subversion 3) that I am working on is receiving a string from a database that is formatted like so: "X-Y-Z" where X, Y, and Z are all numbers. Originally, and for the longest time, X, Y, and Z were all positive numbers. Now there has been a request to allow for negative X's.

An example of the original code is as follows:

my ($X, $Y, $Z) = split("-", $data);

The code would then operate on $X, $Y, and $Z, as well as later using $data once more, in order to provide it's final output. As there is now the possibility of negative values for X, splitting on a "-" has become problematic.

I proposed the following code:

my ($X, $Y, $Z) = ($data =~ /(-?\d*)-(\d*)-(\d*)/);

using a regular expression to more fully describe the data set that is possibly found in $data, however, this has been found to not work exactly the same as the previous usage of split.

My question is: why? Additionally: what can I do to provide the correct splitting that we're looking for here? Thank you in advance.


In reply to split versus =~ by russlo

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.