in reply to Re: How do I read files in a directory and not directories?
in thread How do I read files in a directory and not directories?

my @tags = map { /(.{36})\.txt$/ ? $1 : undef } @files;
should be
my @tags = map { /(.{36})\.txt$/ ? $1 : () } @files;
or else you'll end up with a bunch of undefs in @tags.

>perl -le "print 0 + map undef, 1..2 2 >perl -le "print 0 + map { () } 1..2 0

Replies are listed 'Best First'.
Re^3: How do I read files in a directory and not directories?
by davidrw (Prior) on Mar 10, 2006 at 20:09 UTC
    yeah, good point. i actually almost wrote it as my @tags = grep {defined $_} map { ... } @files; (though i like the ? $1 : () much better).. left it out because, in theory, it should never not match, and if it does fail to match, perhaps (no clue if he does or not) OP wants to trap those cases ...
    foreach my $tag (@tags){ die "found a blank" unless defined $tag; .... }
    (though i guess in that case you can't, very easily, tie the failing tag back to an offending file ... hm.)
      If you remove the undefs, you can still trap that case by using if (@files - @tags)