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..
Occasionally you will see:#!/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
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.(undef, my $ipaddr) = split(/\s+/, $part1);
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |