Multi-dimensional Data Structures

K

Konstantinos

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
 
G

Gunnar Hjalmarsson

Konstantinos said:
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.
 
K

Konstantinos

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
 
R

Richard Gration

"Konstantinos" said:
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();
}
}
}
 
A

A. Sinan Unur

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.

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
 
R

Richard Gration

"Konstantinos" said:
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
 
G

Gunnar Hjalmarsson

[ Please don't top post! ]
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
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

...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-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top