Flatting list references

Discussion in 'Perl Misc' started by Gibbering Poster, Feb 24, 2006.

  1. Hi folks ... I need to completely flatten out nested list references
    into a single-dimension list:

    [[1, 2], [[[3], [4,5], 6], 7, 8], 9, [[[[[0]]]]]] would become
    [1,2,3,4,5,6,7,8,9,0]

    I've tried doing this, but cant seem to find a simple elegant way to
    handle this ...

    Any help is greatly appreciated... thanks!
     
    Gibbering Poster, Feb 24, 2006
    #1
    1. Advertising

  2. Gibbering Poster

    Guest

    "Gibbering Poster" <> wrote:
    > Hi folks ... I need to completely flatten out nested list references
    > into a single-dimension list:
    >
    > [[1, 2], [[[3], [4,5], 6], 7, 8], 9, [[[[[0]]]]]] would become
    > [1,2,3,4,5,6,7,8,9,0]
    >
    > I've tried doing this, but cant seem to find a simple elegant way to
    > handle this ...
    >
    > Any help is greatly appreciated... thanks!


    sub flatten {
    map {ref $_?flatten($_):$_} @{$_[0]}
    };

    my $x=[[1, 2], [[[3], [4,5], 6], 7, 8], 9, [[[[[0]]]]]];
    use Data::Dumper;
    print Dumper [flatten($x)];

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 24, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    446
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    458
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    616
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    720
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    843
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page