Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I tighten this with map?

by SparkeyG (Curate)
on Oct 16, 2001 at 19:29 UTC ( [id://119153]=perlquestion: print w/replies, xml ) Need Help??

SparkeyG has asked for the wisdom of the Perl Monks concerning the following question:

I have a feeling I can tighten this up with map but after doing some reading, I am still a little fuzzy as to how to accomplish this. I humbly ask for help.
opendir(DIR, $directory) or die $!; @files = grep { /^\w{3}\d*:log.$date$/ } readdir(DIR); foreach my $line (@files) { $line =~ m/^(\w{3}\d*):log.$date$/; $fileHash{$1} = $line; } closedir DIR;
--SparkeyG

Replies are listed 'Best First'.
Re: How do I tighten this with map?
by japhy (Canon) on Oct 16, 2001 at 19:50 UTC
    This assumes %fileHash is empty beforehand, and that you meant to escape that . in the regex...
    %fileHash = map { /^(\w\w\w\d*):log\.$date$/ ? ($1, $_) : () } readdir DIR;

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Just curious why you replaced \w{3} with \w\w\w.

        That's a little speed optimisation, with /\w{3}/ the regex engine sees the {} construct and has to start counting. If you write it out it's just three single characters in a row, so no counting => quicker.

        Of course, this could be optimised by the regex compiler but that wasn't the case at the time of writing of Mastering Regular Expressions. At least this is where I got this from. As japhy is more up to date on this I assume he has checked that this optimisation hasn't been done in the meantime.

        -- Hofmator

      Thank you japhy. That's what I was looking for exactly, and I think I understand most of it.

      --SparkeyG

Re: How do I tighten this with map?
by dragonchild (Archbishop) on Oct 16, 2001 at 19:35 UTC
    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.

      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.

Re: How do I tighten this with map?
by Amoe (Friar) on Oct 16, 2001 at 19:41 UTC
    Although I might redeem myself by suggesting that you store your regex with qr// - It's faster if it doesn't have to get compiled however many times. This might mess up the capturing though...hmm...

    --
    my one true love
Re: How do I tighten this with map?
by Amoe (Friar) on Oct 16, 2001 at 19:37 UTC
    map { m/^(\w{3}\d*):log.$date$/ && $fileHash{$1} = $_ } @files; If you really want to use map, although I'm told it's bad form to discard the return value.

    --
    my one true love
      Actually, this is terrible. Ignore me please :)

      --
      my one true love

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found