in reply to Re: Working with a unkown number of arrays
in thread Working with a unkown number of arrays

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: Working with a unkown number of arrays
by chromatic (Archbishop) on Feb 05, 2007 at 19:01 UTC

    Yes, that's much closer! Let me correct a couple of small nits:

            if ($_ =~ /\d*/){    ##if directory name is a number

    The $_ is unnecessary there. Be very careful with that regex, though; it matches strings with zero or more digits. I think you might do better with if (/\.\d+/) {.

    foreach (@directories) { my $dir_num = $_;

    I see this pattern often and don't understand it. It's much simpler to write:

    for my $dir_num (@directories) {

    Of course, you can still use foreach there in place of for if you want, but they're synonyms, so I use the shorter one.

      /\.\d+/

      And based on the patterns (examples) he gave before, I don't think we can assume there's a dot, but we do know the numbers are at the end. Therefore:

      /\d+$/

      Furthermore, the whole thing can be reduced to this:

      my @directories = grep { /\d+$/ and -d $_ } readdir DIR;

      although that doesn't do the print. But I don't think that's necessary.

Re^3: Working with a unkown number of arrays
by wojtyk (Friar) on Feb 05, 2007 at 22:41 UTC
    I still don't understand the need for extra variables. Why create code that creates an array and then iterates over it later when you can just iterate in place without the variable? Aka, this:
    foreach (readdir DIR) { # do stuff }
    seems vastly superior to this:
    foreach (readdir DIR) { ... push(@array, $_); ... } foreach (@array) { # do stuff }
    Why the added step?

    On a side note, I still stand by sorted hashes rather than arrays as the correct way to do this.

A reply falls below the community's threshold of quality. You may see it by logging in.