Skip to content

Instantly share code, notes, and snippets.

@yasu47b
Last active March 21, 2017 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasu47b/a8ca9248c968e8a80dd40ae05dcda973 to your computer and use it in GitHub Desktop.
Save yasu47b/a8ca9248c968e8a80dd40ae05dcda973 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
my $arr = [[1,2,3],[4,5,[6,7,[8,9,[1,2,3]]]]];
my @arr = ([1,2,3],[4,5,[6,7,[8,9,[1,2,3]]]]);
sub flatten {
my $arg = @_ > 1 ? [@_] : shift;
my @tmp = ref $arg eq 'ARRAY' ? @$arg : $arg;
my @output = map {ref $_ eq 'ARRAY' ? flatten($_) : $_} @tmp;
return @output;
}
my @output = flatten($arr);
my @output2 = flatten(@arr);
say "@output";
say "@output2";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment