develooper Front page | perl.beginners | Postings from February 2009

Re: how to do this in one line (with map?)

Thread Previous
John W. Krahn
February 18, 2009 22:31
Re: how to do this in one line (with map?)
Message ID:
Rob Dixon wrote:
> John W. Krahn wrote:
>> wrote:
>>> I want to extract a list of postfix's queue id's.  The 'mailq' command
>>> returns output as follows:
>>> right now I'm doing the following:
>>> my @ids;
>>> foreach (`mailq`) {
>>>     next unless /^\w+/;
>>>     chomp;
>>>     push @ids, (split)[0];
>>> }
>>> I know it can be done with one line.  Here's what I've got so far:
>>> my @ids = map {chomp; (split)[0] if /^\w/} `mailq`;
>> You don't need chomp() because split() removes *all* whitespace, and 
>> besides you are only using the first field.
>>> This 'kinda' works, but it also adds a bunch of blank elements to the
>>> @ids array.  What am I doing wrong?
>> my @ids = map /^\w/ ? (split)[0] : (), `mailq`;
>   my @ids = map /^(\w+)/, `mailq`;

Not the same thing.  Possibly:

my @ids = map /^(\S+)/, `mailq`;


my @ids = map /^(\w\S*)/, `mailq`;


my @ids = `mailq` =~ /^\w\S*/mg;

Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About