in reply to How to flatten an x-dimensional array?

And now for something completely lispish :-)

my @ar = grep length, flatten(@array); print Dumper(@ar); sub flatten{ return unless @_; my ($car,@cdr) = @_; if(ref($car) eq 'ARRAY'){ flatten(@$car, @cdr); } else { $car, flatten(@cdr); } }

Replies are listed 'Best First'.
Re: Re: How to flatten an x-dimensional array?
by dragonchild (Archbishop) on Mar 12, 2002 at 14:49 UTC
    To Perl-ize this wonderful Lisp solution *grins*
    sub flatten { return unless @_; my $val = shift; return flatten(@$val, @_) if UNIVERSAL::isa($val, 'ARRAY'); return $val, flatten(@_); }

    ------
    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: Re: How to flatten an x-dimensional array?
by Sidhekin (Priest) on Mar 12, 2002 at 11:49 UTC

    ++abstracts -- ($car, @cdr) indeed! :-)

    The Sidhekin
    print "Just another Perl ${\(trickster and hacker)},"