Multi-dimensional Data Structures

Discussion in 'Perl Misc' started by Konstantinos, Dec 9, 2003.

  1. Konstantinos

    Konstantinos Guest

    Hi all,

    I need help in the following topic :

    I create a Multi-dimensional Data Structure within a subroutine, and I
    want to keep calling this subroutine recursively, passing each time the
    MD-data structure. How am I gonna achieve this ? Since the references to
    memory adresses for this MD-data structure are created within the scope
    of the subroutine, aren't these memory slots emptied each time the sub
    ends ? (I think that is my problem, and my script does not work).

    Thank ya all,

    Konstantinos
     
    Konstantinos, Dec 9, 2003
    #1
    1. Advertising

  2. Konstantinos wrote:
    > I create a Multi-dimensional Data Structure within a subroutine,
    > and I want to keep calling this subroutine recursively, passing
    > each time the MD-data structure. How am I gonna achieve this ?
    > Since the references to memory adresses for this MD-data structure
    > are created within the scope of the subroutine, aren't these memory
    > slots emptied each time the sub ends ? (I think that is my problem,
    > and my script does not work).


    Which script? Would you mind sharing a minimal program that
    illustrates the problem?

    Until we have seen that, the only thing I can say is that you should
    probably declare a reference to the data structure somewhere outside
    the sub.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 9, 2003
    #2
    1. Advertising

  3. Konstantinos

    Konstantinos Guest

    It a program that you can say creates something like a binary tree;
    It not exactly that, because it is used to classify biological samples,
    based on their gene expression data. The difference between regular
    binary trees is that it does not use a single value as discriminator at
    each split of the parent node to the daughter ones, but a range of
    values-attributes instead. The problem arises from the fact, that the
    first time I call the sub, a parent node splits to the left and right
    daughter ones which are stored as Multi-D structure CREATED INSIDE THE
    SUB. Then I have to use these left and right daughter nodes as parental
    this time, to make further splits of the tree nodes, by calling
    recursively the sub, and feeding these daughter nodes again to the
    sub...But since they are created inside the scope of the sub the first
    time it is called, how can they be fed again to it in a next call ? (I
    try to send them when I do recursive calls of the sub, by using pass by
    reference-> a reference TO THIS STRUCTURES CREATED INSIDE THE SUB, but
    they do not seem to get into the next call of the sub)... When you do
    recursive call, aren't you supposed to GET OUT of the sub, and then call
    it and GET IN AGAIN, so as you go out, any memory addresses that are
    made in the scope of the sub are released-emptied ?

    Cheers,

    Konstantinos


    Gunnar Hjalmarsson wrote:

    > Konstantinos wrote:
    >
    >> I create a Multi-dimensional Data Structure within a subroutine,
    >> and I want to keep calling this subroutine recursively, passing
    >> each time the MD-data structure. How am I gonna achieve this ?
    >> Since the references to memory adresses for this MD-data structure
    >> are created within the scope of the subroutine, aren't these memory
    >> slots emptied each time the sub ends ? (I think that is my problem,
    >> and my script does not work).

    >
    >
    > Which script? Would you mind sharing a minimal program that
    > illustrates the problem?
    >
    > Until we have seen that, the only thing I can say is that you should
    > probably declare a reference to the data structure somewhere outside
    > the sub.
    >
     
    Konstantinos, Dec 9, 2003
    #3
  4. In article <br4tka$6a9$>, "Konstantinos"
    <> wrote:

    > Hi all,
    >
    > I need help in the following topic :
    > I create a Multi-dimensional Data Structure within a subroutine, and I
    > want to keep calling this subroutine recursively, passing each time the
    > MD-data structure. How am I gonna achieve this ? Since the references to
    > memory adresses for this MD-data structure are created within the scope
    > of the subroutine, aren't these memory slots emptied each time the sub
    > ends ? (I think that is my problem, and my script does not work).
    > Thank ya all,
    > Konstantinos
    >


    Create the Multi-dimensional Data Structure (tm) (IOW your top level hash
    or array) in the calling code and pass a reference to it into the sub. I
    believe this is called re-entrant coding.

    Rich

    eg. (untested, but will create you a 10_000 element structure)

    #!/usr/bin/perl
    use strict;

    my $mdds = [];

    stuff($mdds);

    sub stuff {
    my $ref = shift;
    my ($n1,$n2);
    foreach $n1 (1 .. 100) {
    foreach $n2 (1 .. 100) {
    $ref->[$n1]->[$n2] = rand();
    }
    }
    }
     
    Richard Gration, Dec 9, 2003
    #4
  5. Do not top post
    Do not blindly quote messages in full
    Do read the posting quidelines:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    And it seems like you might benefit from reading:
    http://www.catb.org/~esr/faqs/smart-questions.html
    as well.

    Konstantinos <> wrote in news:br51u4$clp$1
    @solaris.cc.vt.edu:

    > Gunnar Hjalmarsson wrote:
    >
    >> Konstantinos wrote:
    >>
    >>> I create a Multi-dimensional Data Structure within a subroutine,
    >>> and I want to keep calling this subroutine recursively, passing
    >>> each time the MD-data structure. How am I gonna achieve this ?
    >>> Since the references to memory adresses for this MD-data structure
    >>> are created within the scope of the subroutine, aren't these memory
    >>> slots emptied each time the sub ends ? (I think that is my problem,
    >>> and my script does not work).

    >>
    >>
    >> Which script? Would you mind sharing a minimal program that
    >> illustrates the problem?

    >
    > It a program that you can say creates something like a binary tree;


    Please follow Gunnar's suggestion and post a minimal script that
    illustrates your problem.

    > The problem arises from the fact, that the first time I call the sub, a
    > parent node splits to the left and right daughter ones which are stored
    > as Multi-D structure CREATED INSIDE THE SUB. Then I have to use these
    > left and right daughter nodes as parental this time, to make further


    Does the following link help?
    http://iis1.cps.unizar.es/Oreilly/perl/cookbook/ch11_16.htm
    --
    A. Sinan Unur

    Remove dashes for address
    Spam bait: mailto:
     
    A. Sinan Unur, Dec 9, 2003
    #5
  6. In article <br51u4$clp$>, "Konstantinos"
    <> wrote:

    > The problem arises from the fact, that the
    > first time I call the sub, a parent node splits to the left and right
    > daughter ones which are stored as Multi-D structure CREATED INSIDE THE
    > SUB.


    This isn't quite correct, conceptually. You only have one data structure
    (you _should_ only have one, otherwise you _will_ have the kind of
    problems I'm about to say that you don't), your sub is placing data into
    that data structure, and extending the data structure where necessary.
    Your sub is not _creating_ the daughter structures in the sense you mean.
    It is creating them in another sense, but once they are assigned to a
    location in the parent structure they will survive until the parent
    structure is undef'ed, unless you try really hard to make them disappear
    before that.

    Perl deals in scalar values (SVs) and refcounts. A scalar value is a
    container for a datum. This datum could be a number, a string, the
    undefined value, or a reference to another scalar value, or a reference
    to hash or array or ... etc. A variable name is a way of referencing that
    scalar value. But there are other ways. Consider:

    $c = aaa();
    print $$c;

    sub aaa {
    my @a = (1,2);
    $b = \$a[1]; # <--- interesting bit
    return $b;
    }

    This code will output "2". Why? Because the interesting bit creates
    another way to get at the data stored in the array. It creates another
    reference to the scalar value which is accessible as the second element
    of the array, therefore the refcount of this SV is increased by one. When
    the array @a goes out of scope when the sub exits, the refcount of $a[0],
    the first element, is decreased by one and ends up 0, so that SV goes
    away. The refcount of $a[1] is decreased by one, but this time it ends up
    1, because of the reference the interesting bit created, so it is not
    destroyed.

    If all of this is confusing then read "perldoc perlref" until your head
    hurts. Then read it some more.

    HTH
    Rich
     
    Richard Gration, Dec 9, 2003
    #6
  7. [ Please don't top post! ]

    Konstantinos wrote:
    > Gunnar Hjalmarsson wrote:
    >> Konstantinos wrote:
    >>> I create a Multi-dimensional Data Structure within a
    >>> subroutine, and I want to keep calling this subroutine
    >>> recursively, passing each time the MD-data structure. How am I
    >>> gonna achieve this ? Since the references to memory adresses
    >>> for this MD-data structure are created within the scope of the
    >>> subroutine, aren't these memory slots emptied each time the sub
    >>> ends ? (I think that is my problem, and my script does not
    >>> work).

    >>
    >> Which script? Would you mind sharing a minimal program that
    >> illustrates the problem?
    >>
    >> Until we have seen that, the only thing I can say is that you
    >> should probably declare a reference to the data structure
    >> somewhere outside the sub.

    >
    > It a program that you can say ...


    <long description in English snipped>

    Why on earth can't you speak Perl??

    This script matches your first description. It might be of some help:

    my $mdref; # variable declaration

    $mdref = addarray($mdref); # initial structure created
    print "@{$mdref->[$_]}\n" for (0..$#$mdref);
    print "\n";

    $mdref = addarray($mdref); # another anonymous array added
    print "@{$mdref->[$_]}\n" for (0..$#$mdref);

    sub addarray {
    my $ref = ( shift or [] );
    my ($i, $x);
    if ($ref->[0]) {
    $i = $#$ref + 1;
    $x = $ref->[$#$ref][2] + 1;
    } else {
    $i = 0;
    $x = 1;
    }
    $ref->[$i] = [$x, $x+1, $x+2];
    $ref
    }

    It outputs:
    1 2 3

    1 2 3
    4 5 6

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 9, 2003
    #7
  8. Konstantinos

    Konstantinos Guest

    Konstantinos, Dec 9, 2003
    #8
  9. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Konstantinos <> wrote in
    news:br4tka$6a9$:

    > ...Since the references to
    > memory adresses for this MD-data structure are created within the

    scope
    > of the subroutine, aren't these memory slots emptied each time the

    sub
    > ends ? (I think that is my problem, and my script does not work).


    In Perl, memory is not reclaimed when variables go out of scope, but
    rather when there are no extant references to the variables. In C,
    it's very bad to use a pointer to an auto variable after the function
    has ended; in Perl, it's perfectly fine to use a reference to a my
    variable after the function (or other scope) has ended.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

    iD8DBQE/1xB2Y96i4h5M0egRAoLSAJ4/xsOAqsdUGFTIAmS9vxP5MIUCXACfdpfr
    IIQZ4A2u3w50nOrzcJECU5k=
    =eymj
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Dec 10, 2003
    #9
    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. Alf P. Steinbach
    Replies:
    0
    Views:
    441
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,935
    Default User
    Aug 19, 2003
  3. anthonyberet

    2-dimensional data structures

    anthonyberet, Jan 26, 2006, in forum: Python
    Replies:
    15
    Views:
    592
    Gerard Flanagan
    Feb 19, 2006
  4. Alfonso Morra
    Replies:
    11
    Views:
    728
    Emmanuel Delahaye
    Sep 24, 2005
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    210
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page