develooper Front page | perl.beginners | Postings from April 2008

Re: Array element split, with grep

Thread Previous | Thread Next
From:
John W. Krahn
Date:
April 30, 2008 14:47
Subject:
Re: Array element split, with grep
Rob Dixon wrote:
> Steve Bertrand wrote:
>> Can someone explain to me how I can fix this up to achieve my desired 
>> results?
>>
>> my $time = (split (/:/, (grep (/^info/, @contents))))[0];
>>
>> A sample snip of data:
>>
>> this382:3828
>> info447:4729
>> that274:9294
>>
>> ...and I just want the $time to become info447.
>>
>> The way I think it works is:
>>
>> - grep out the single entry from the @contents array
>> - split that entry on the : delimiter
>> - insert into $time the first element of the split
>>
>> I can do it with more than one line, but I've become accustomed to using 
>> split in array fashion, however, never with grep before.
> 
>  my ($time) = grep /^info/, map /([^:]*)/, @contents;

No need for grep():

my ($time) = map /^(info[^:]*)/, @contents;


John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

Thread Previous | Thread Next


Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About