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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

make code into a 1 liner 4

Status
Not open for further replies.

onomo

Programmer
Jul 4, 2011
1
GB
Hi all, i have this code to extract all words beginning with an "@" character, it works perfectly fine:
Code:
push @text,map(/(?<!\w)@\w+/g, @words);
but, i need to avoid adding replicas of the same word. my space is very limited and i was wondering if it was possible to do this in a 1 liner, extract those words and avoiding replicas?


thanks!
 
I thought you should be able to do something like this (loosely based on some map examples on perldoc -f map):

Perl:
%hash = map { /(?<!\w)@\w+/g => 1 } @words;

And then print the hash keys out... but it doesn't work. I'd be keen to know why; I'm assuming it's not handling the situation where the expression doesn't match correctly and adding a single value to the list rather than a key, value pair?

Annihilannic.
 
Huh? Space is limited? This isn't homework is it? Combining all this into one line seems like an unnecessary complication.

Anyway, here's one way to do it:
Code:
push @text, map {keys %{$_}} {map {$_, 1} grep(/^@/, @words)};
Since that code uses a hash, the order of the elements cannot be guaranteed.
 
rhash, does your method not omit duplicates, but simply overwrites any duplicate with the same key/value, in essence only leaving you with one key/value pair?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
1DMF, you're right - that is pretty much the case. An anonymous hash is created with keys that are all the elements from @words[] that match the criteria in the grep expression. So what gets added to @text[] are the keys from that anonymous hash -- the unique elements returned from the grep.

Wasn't that what the OP asked for? Or am I missing something?
 
Yep, that's what they were looking for.

What incorrect assumptions was I making in my attemp, rharsh?

Annihilannic.
 
Thanks guys, just wanted to be sure I'd got my head round the code and understood exactly what was happening.

Have some stars :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Annihilannic said:
I thought you should be able to do something like this (loosely based on some map examples on perldoc -f map):
Code:
%hash = map { /(?<!\w)@\w+/g => 1 } @words;

@Anni,

The results of that particular regex will be an array, so you must instead use two maps to create the hash:

Code:
%hash = map {$_ => 1} map {/(?<!\w)@\w+/g} @words;

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top