You are very close and I don't see anything wrong with this approach.

The first split is in a list context and there is no need to assign "junk". The 2nd split should also be put into a list context and then just take the 2nd thing via a "list slice", [1] subscript. See below..

#!/usr/bin/perl -w use strict; my $line = 'client 127.0.0.1#47560: query: host.example.com IN AAAA'; my ( $part1) = split (/#/,$line); my ( $ipaddr ) = (split(/\s+/, $part1))[1]; print " part1: $part1\n ipaddr: $ipaddr\n"; __END__ Prints: part1: client 127.0.0.1 ipaddr: 127.0.0.1
Occasionally you will see:
(undef, my $ipaddr) = split(/\s+/, $part1);
And although that will work, I wouldn't use it. Assigning something to undef as a lvalue "throws it in the bit bucket". I think it is "cleaner" to use the slice.

Update: Now of course, if you just want the IP address:

my $line = 'client 127.0.0.1#47560: query: host.example.com IN AAAA'; (my $ipaddr) = (split/[\s#]/,$line)[1]; print "$ipaddr\n"; __END__ prints: 127.0.0.1

In reply to Re: split split by Marshall
in thread split split by Plankton

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.