You have two problems here. The first is the unescaped special character / as your split pattern, and the second is the use of a scalar variable in list context (already commented upon in other answers to your question).

You need to remove the special meaning from / in order for split to be able to use it. Single quotes, which suppress interpolation, is the easiest on the eyes. You could also use "\/", /\//, m|/|, etc., consult perldoc perlre or your favorite perl reference for details.

Next, by default split returns a list of strings. You either need to catch these with an array and then use the first item in that array ($cnID[0]), or assign your scalar $cnID to the first member of the list explicitly.

This is similar to podmaster's answer, but keeps the list on the right hand side of the assignment:  my $cnID = (split '/', $serverID,2)[0]; The last argument to split tells split to break $serverID into no more than 2 pieces - no need to bother with any more, given that you only want the first piece anyway.

Originally posted as a Categorized Answer.


In reply to Re: Splitting a string... Duh? by virtualsue
in thread Splitting a string... Duh? by KenShackelford

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.