Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Multi-dimensional Data Structures

Reply
Thread Tools

Multi-dimensional Data Structures

 
 
Konstantinos
Guest
Posts: n/a
 
      12-09-2003
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

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-09-2003
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

 
Reply With Quote
 
 
 
 
Konstantinos
Guest
Posts: n/a
 
      12-09-2003

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


 
Reply With Quote
 
Richard Gration
Guest
Posts: n/a
 
      12-09-2003
In article <br4tka$6a9$(E-Mail Removed)>, "Konstantinos"
<(E-Mail Removed)> 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();
}
}
}
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-09-2003
Do not top post
Do not blindly quote messages in full
Do read the posting quidelines:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

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

Konstantinos <(E-Mail Removed)> 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/pe...ok/ch11_16.htm
--
A. Sinan Unur
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove dashes for address
Spam bait: (E-Mail Removed)
 
Reply With Quote
 
Richard Gration
Guest
Posts: n/a
 
      12-09-2003
In article <br51u4$clp$(E-Mail Removed)>, "Konstantinos"
<(E-Mail Removed)> 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-09-2003
[ 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

 
Reply With Quote
 
Konstantinos
Guest
Posts: n/a
 
      12-09-2003
A. Sinan Unur wrote:

> Do not top post
> Do not blindly quote messages in full
> Do read the posting quidelines:
> http://mail.augustmail.com/~tadmc/cl...uidelines.html
>
> And it seems like you might benefit from reading:
> http://www.catb.org/~esr/faqs/smart-questions.html
> as well.
>
> ...


Thank you. That was very educative.

Konstantinos

 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      12-10-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Konstantinos <(E-Mail Removed)> wrote in
news:br4tka$6a9$(E-Mail Removed):

> ...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-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM
data structures in java reference learningjava Java 3 10-16-2003 07:31 AM
Data Structures help MIke Beam Java 5 10-14-2003 09:53 AM
sharing complex data structures between threads in mod_perl2 Dennis Gavrilov Perl 1 07-24-2003 07:22 AM



Advertisments