CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
help with obtimizing

 



soeeom
New User

Jul 4, 2011, 1:03 PM

Post #1 of 2 (779 views)
help with obtimizing Can't Post

Hi guys, I'm trying to write some code that extracts all words beginning with an "@" and place them into an array, i've done this and it works fine:

Code
push @text,map(/(?<!\w)@\w+/g, @words);


but i need to to weed out replicas, i can do this easily with extra lines of code but i need to keep the code as small as possible, i know perl is very good at this so i was wondering if i could do this in a kinda obtimized 1 liner?


BillKSmith
Veteran

Jul 4, 2011, 8:47 PM

Post #2 of 2 (766 views)
Re: [soeeom] help with obtimizing [In reply to] Can't Post

I am not sure what you mean by 'weed out replicas'. When a word is replicated in the original, do you want to remove all copies of it or all but one. Assuming the later, use the uniq function in the module List::MoreUtils.

untested.


Code
   

use List::MoreUtils qw (uniq);

@text = uniq grep {/^@/} @words;





This solution assumes one word perl element of @words. Your code suggests that this is not true. Use your code. Filter your results with uniq on a separate line.
Good Luck,
Bill

(This post was edited by BillKSmith on Jul 5, 2011, 4:56 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives