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

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

Thread Previous | Thread Next
From:
Rob Dixon
Date:
February 18, 2009 18:32
Subject:
Re: how to do this in one line (with map?)
Message ID:
499CC4AF.8010405@gmx.com
John W. Krahn wrote:
> pablo@compugenic.com wrote:
>> I want to extract a list of postfix's queue id's.  The 'mailq' command
>> returns output as follows:
>>
>> --
>> 8C0597408D     2503 Tue Feb 17 16:15:34  Karin@domain.org
>>     (connect to bar-plate.com[209.62.20.192]:25: Connection refused)
>> 			    staffords@bar-plate.com
>>
>> 88E8E7409E 1881 Tue Feb 17 23:37:40 Dianne@domain.org
>>     (connect to mail.fbig.com[70.68.160.253]:25: Connection timed out) 
>> 			    persistencer5345@fbig.com
>> --
>>
>> 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`;

Rob

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About