To just get one item, use list slice.

A very handy technique. Note that the order of the vars on the left don't have to be in the same order that they are in the input string (the indicies to the slice can be in any order).

I usually order the vars on the left to be in the order that they will be used in the code that follows the split. I fiddle with the indices on the right to make that possible. Also here I used a -1 instead of 2 just to show that is possible (-1 means the last thing in list)

Oh, PS, yes it is possible to write a single char like ':' to use in the split instead of /:/, but I always use the regex form.

#!/usr/bin/perl -w use strict; my $x = 'R4:abcxyz45:LNX'; my $server = (split(/:/,$x))[1]; print "$server\n"; #prints abcxyz45 (my $OS, $server) = (split(/:/,$x))[-1,1]; print "$OS $server\n"; #prints LNX abcxyz45

In reply to Re^2: Regexp help by Marshall
in thread Regexp help by Anonymous Monk

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.