Your code is doing the following:

  1. Find all the elements of @lines that begin with AUID (plus some optional whitespace) and store them in @headings,
  2. take the first found element and do a split on ' ' (which has a special meaning as explained in its docs) and overwrite the contents of @headings with the result, and
  3. shift the first element off @headings and discard it.

There are two problems with the oneliner you showed:

<update2> Eily's suggestion is nicest IMO:

my (undef, @headings) = split ' ', first {/^\s*AUID/} @lines;

</update2>

You can pack it in a single line, here is one way, but it's kind of ugly. This uses (...)[0] to return the first return value of grep and splice to return the list without its first element:

my @headings = splice @{[ split ' ', (grep /^\s*AUID/, @lines)[0] ]}, 1;

Although I might recommend using first from List::Util to avoid the wasteful grepping of the entire array:

my @headings = splice @{[ split ' ', first {/^\s*AUID/} @lines ]}, 1;

I still don't think this is very pretty because of the @{[]} trick I have to use to get splice to be happy. This one, using map and a regex, I like better:

my @headings = map {/(?!^\s*AUID)\s(\S+)/g} first {/^\s*AUID/} @lines;

Update: map isn't really needed:

my @headings = (first {/^\s*AUID/} @lines)=~/(?!^\s*AUID)\s(\S+)/g;
use warnings; use strict; use Test::More tests=>6; use List::Util qw/first/; my @lines = ("AUID \cIRA \cIDec \cILabel \cIV \cIB-V \cIComments", 'hello','world'); my @expect = ('RA', 'Dec', 'Label', 'V', 'B-V', 'Comments'); { my @headings = grep /^\s*AUID/, @lines; @headings = split ' ', $headings[0]; shift @headings; is_deeply \@headings, \@expect or diag explain \@headings; } { my @headings = splice @{[ split ' ', (grep /^\s*AUID/, @lines)[0] ]}, 1; is_deeply \@headings, \@expect or diag explain \@headings; } { my @headings = splice @{[ split ' ', first {/^\s*AUID/} @lines ]}, 1; is_deeply \@headings, \@expect or diag explain \@headings; } { my @headings = map {/(?!^\s*AUID)\s(\S+)/g} first {/^\s*AUID/} @lines; is_deeply \@headings, \@expect or diag explain \@headings; } { my @headings = (first {/^\s*AUID/} @lines)=~/(?!^\s*AUID)\s(\S+)/g +; is_deeply \@headings, \@expect or diag explain \@headings; } { my (undef, @headings) = split ' ', first {/^\s*AUID/} @lines; is_deeply \@headings, \@expect or diag explain \@headings; }

In reply to Re: shift split grep question (updated) by haukex
in thread shift split grep question by Xilman

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.