Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filter array? 2

Status
Not open for further replies.

webamoeba

Programmer
Sep 6, 2006
23
GB
Hi,

I want to filter out strings in an array that do not start with the word unit. e.g.

unit1,
foo,
unitYeeHar,
bar,
Monky,
unit,
aunit

Becomes

unit1,
unitYeeHar,
unit

Is there a better way of doing this other then iterating through each element?

Thanks
 
@array = grep {/^unit/} @array;


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Use grep. It still iterates over all the members in the array under the covers, of course, but the code is cleaner.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thankyou!! :D

Darn great big O'Reilly Programming Perl book doesn't cover grep grrrrr. :S
 
>> Is there a better way of doing this other then iterating through each element?

No. In fact there is no way of doing this without iterating over each element of the array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top