Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How do I tighten this with map?

by dragonchild (Archbishop)
on Oct 16, 2001 at 19:35 UTC ( [id://119157]=note: print w/replies, xml ) Need Help??


in reply to How do I tighten this with map?

Adding some safety features (like my) as well as the map gives us ...
opendir(DIR, $directory) or die $!; my @files = grep { /^\w{3}\d*:log.$date$/ } readdir(DIR); my %fileHash; %fileHash = map { /^(\w{3}\d*):log.$date$/; ($1 => $_) } @files; closedir DIR;

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re: How do I tighten this with map?
by IraTarball (Monk) on Oct 16, 2001 at 20:08 UTC
    I'll bet dragonchild thought of this and omitted it for some reason. Not being as wise I'll go ahead and put it out there. This is the same solution without the temporary @files array.
    opendir(DIR, $directory) or die $!; my %fileHash = map { (/^(\w{3}\d*):log\.$date$/ => $_); } grep /^\w{3}\d*:log\.$date$/, readdir(DIR); closedir DIR;
    I also took the liberty of adding a '\' before the '.' in your regex. It looked like you meant a literal '.' not 'any character'. I think dragonchild's is more easily understood but I like this one.

    UPDATE: Even though this works and is all true, look a little lower to japhy's. It's better.

    Ira,

    "So... What do all these little arrows mean?"
    ~unknown

      The reason I put the temporary @files array is because it might not be temporary. Also, it enhances readability. Just cause you can doesn't mean you should. :-)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Of course you are right. I thought this same thing, and tried to allude to these problems in my post. But I am young and brash and still try to reduce, perhaps too far.

        Mostly because it's fun.

        On an aside, if the @files array is not needed then japhy's solution which removes one whole regex evaluation must be considered better by some definition of efficiency. Those things are expensive.

        Ira,

        "My thoughts are my own, I think."
        ~Me, I think

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-20 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found