How to create a self-referring datastructure in one line

Discussion in 'Perl Misc' started by jdamon@gmail.com, Feb 9, 2007.

  1. Guest

    Hi,

    I'm trying to create a self referential datastructure in one line.


    An example is :



    my %tree;
    %tree = (
    root => {
    parent1 => {
    child => {
    data => "foo",
    root => $tree{root} # or even \%{$tree{root}}
    }
    }
    }
    );

    The problem is that the reference doesn't exist at the time that the
    structure is created.

    If you are going to post back to this , I DON'T want the solution when
    you build this up iteratively. I just want to know if it can be done
    in a declaration like stated above.
    , Feb 9, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > I'm trying to create a self referential datastructure in one line.


    Why?
    > An example is :
    >
    >
    >
    > my %tree;
    > %tree = (
    > root => {
    > parent1 => {
    > child => {
    > data => "foo",
    > root => $tree{root} # or even \%{$tree{root}}
    > }
    > }
    > }
    > );
    >
    > The problem is that the reference doesn't exist at the time that the
    > structure is created.


    use strict; use warnings; my $var; $var = \$var; print "$var ${$var}\n";


    Or am I missing the point?
    Mark Clements, Feb 9, 2007
    #2
    1. Advertising

  3. Mirco Wahab Guest

    wrote:
    >
    > $_->{root} = {
    > parent => {
    > child => {
    > data => 'foo',
    > root => \$$_{root}
    > }
    > }
    > } for \my %tree;
    >
    > I can't imagine why a one-liner requirement is necessary, however.


    Interesting Idea. That one leads directly to the
    'one expression initialization' (what I think was meant):

    ...

    my %tree = (
    map {
    $_->{root} = {

    parent => {
    child => {
    data => 'foo',
    root => \$$_{root}
    }
    }

    };
    %$_
    } {}
    );

    ...

    Thanks for this nice idea (didn't know
    "this way" of post instantiation-
    initialization of hashes)

    Regards

    M.
    Mirco Wahab, Feb 10, 2007
    #3
  4. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > I'm trying to create a self referential datastructure in one line.
    >
    >
    > An example is :
    >
    >
    >
    > my %tree;
    > %tree = (
    > root => {
    > parent1 => {
    > child => {
    > data => "foo",
    > root => $tree{root} # or even \%{$tree{root}}
    > }
    > }
    > }
    > );
    >
    > The problem is that the reference doesn't exist at the time that the
    > structure is created.
    >
    > If you are going to post back to this , I DON'T want the solution when
    > you build this up iteratively. I just want to know if it can be done
    > in a declaration like stated above.


    How absolutely charming.

    Build the data structure you want in the way you have forbidden to post
    about. Then use Data::Dumper on it to see how to rebuild it in a
    single statement.

    Anno
    -berlin.de, Feb 10, 2007
    #4
    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. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    552
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    571
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    383
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    354
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    377
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page