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:??; | [reply] [d/l] |
|
Just curious why you replaced \w{3} with \w\w\w.
| [reply] |
|
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
| [reply] [d/l] |
|
Thank you japhy. That's what I was looking for exactly, and I think I understand most of it.
--SparkeyG
| [reply] |
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. | [reply] [d/l] |
|
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
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
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 | [reply] |
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 | [reply] [d/l] |
|
Actually, this is terrible. Ignore me please :)
--
my one true love
| [reply] |