in reply to Re: Producing 2 lists from a grep call
in thread Producing 2 lists from a grep call

No need for a void map.
for (readdir DIR) { next if $_ eq "." or $_ eq ".."; next unless -d "$base/$_" or -f _; push @{ -d _ ? \@dirs : \@files }, $_; }
Update: Excellent point by particle++. Code updated accordingly.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Re: Producing 2 lists from a grep call
by BrowserUk (Patriarch) on Jun 16, 2002 at 23:23 UTC

    I quite like that one. I came close to that but missed (actually, simply wasn't aware of) the @{\@array) 'trick'.

    However (you knew that was coming right?), as I can do:

    my (@a1,@a2) = ([1,2,3],[3,2,1]);

    Update: the above line doesn't work after all

    I had tried:

    my (@a1,@a2) = ([1,2,3,4,5,6,7],[7,6,5,4,3,2,1]); print Dumper(@a1), Dumper(@a2);

    which gave:

    $VAR1 = [ 1, 2, 3, 4, 5, 6, 7 ]; $VAR2 = [ 7, 6, 5, 4, 3, 2, 1 ];
    which certainly lookedlike it had worked! It was only after seeing this that I went back and added a couple of labels

    print "a1\n", Dumper(@a1), "a2\n", Dumper(@a2);

    That I saw what tstock meant below.

    Thanks tstock, I'll be more careful in future with my quick tests. (I agree about the loop/if being better too.)

    end of update

    Which personally I find a very clear yet concise way of declaring and initialising two arrays. It still seems as if something close to:

    my ( @dirs, @files)= ( @{-d}, @{-f} ) for (readdir DIR);

    could be possible if I could only get the syntactic sugar right!

    This is only an exercise in my trying to understand arrays and list contexts etc. ie. Its essentially a purely academic excercise for late (for me) on a Sunday evening, and so not worthy of anyone's time unless they are also in play mode.

      However (you knew that was coming right?), as I can do:
      my (@a1,@a2) = ([1,2,3],[3,2,1]);

      This doesn't work, sorry. You get @a1 with two array references and @a2 undefined.

      tstock

      Update : This is your one liner: (someone please -- me down for posting this)
      map { ($_ ne '.') && ($_ ne '..') && push @{ -d "$base/$_" ? \@dirs : \@files }, $_ } (readdir DIR);
      I think we can agree that the FOR LOOP/IF STATEMENT is probably a nicer solution ? :)
        I think we can agree that the FOR LOOP/IF STATEMENT is probably a nicer solution ? :)
        Especially since it's almost the for loop I posted - just s/map/for/, pull the (readdir DIR) up top, slip a next into the loop, and there you have it.

        If you actually want something that's not a for loop in disguise, you could pull a really horrible trick like
        my @dir; my @files = map { ($_ ne '.') && ($_ ne '..') && -d "$base/$_" ? do { push @dir, $_; () } : -f "$base/$_" && $_ } readdir DIR;
        But if the Communications Decency Act had gone through I wouldn't have been able to even post this.

        Makeshifts last the longest.

Re^3: Producing 2 lists from a grep call
by particle (Vicar) on Jun 17, 2002 at 12:26 UTC
    this code does not work -- !-d  !=  -f!

    to implement this code properly, you must test for both -d, and -f, and ignore the rest. add next unless -d $_ or -f _; before the push statement and you'll have what you're looking for.

    for (readdir DIR) { next if $_ eq '.' or $_ eq '..'; next unless -d $base . '/' . $_ or -f _; push @{ -d $base . '/' . $_ ? \@dirs : \@files }, $_; }
    Update: changes as per Screamer

    Update 2: for more info on filetests, see -X under perlfunc

    ~Particle *accelerates*

      next unless -d $_ or -f _; Careful, it's "$base/$_". Excellent point though.

      Makeshifts last the longest.