Last active
March 21, 2017 05:03
-
-
Save yasu47b/a8ca9248c968e8a80dd40ae05dcda973 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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