Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re: removal of dupes using a hash

by jarich (Curate)
on May 21, 2004 at 01:05 UTC ( [id://355143]=note: print w/replies, xml ) Need Help??


in reply to Re: removal of dupes using a hash
in thread removal of dupes using a hash

The specifics in this reply are slightly less than ideal. When Perl sees you using $` and $' even once it assumes you might want to use these variables for each and every regular expression thereafter.

This is considered a bad thing because Perl will then copy the prematched text and the postmatched text into $` and $' every time it sees a regular expression. This is okay in this kind of example because the data we're dealing with appears to be small. However it rapidly becomes inefficient once we start dealing with longer strings.

A similar, slightly more efficient version can be written:

foreach (@sorted_data) { /(.*?)\.(.*)/; $hash{$1} = $2; } # print hash here by key value - only latest entry exists

It may also be worth wondering why a regular expression is needed at all:

foreach (@sorted_data) { my ($key, $value) = split /\./, $_, 2; $hash{$key} = $value; } # print hash here by key value - only latest entry exists

Hope this helps,

jarich

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://355143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-18 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found