Efficent data storage

C

Cognition Peon

Hi,

I have used a hash to store relations between persons and their parents
in a study that we are doing.. The relations are defined by the following
conditions..

If Person = 1 or 4-19, then Father = 3 and Mother = 2
If Person = 2, then Father = 23 and Mother = 22
If Person = 3, then Father = 33 and Mother = 32
If Person = 22,23,32 or 33, Father and Mother = 0

Following is the hash that I have used.

my %relations = (
1 => { Father => 3, Mother => 2 },
2 => { Father => 23, Mother => 22 },
3 => { Father => 33, Mother => 32 },
22 => { Father => 0, Mother => 0 },
);
$relations{'23'} = $relations{'32'} = $relations{'33'} = $relations{'22'};
for my $person ( 4 .. 19 ) {
$relations{$person} = $relations{1};
}

I am sure there will be a better ways to do it.. Any help will be greatly
appreciated..

Thanks,
 
T

Tore Aursand

I have used a hash to store relations between persons and their parents
in a study that we are doing.. The relations are defined by the following
conditions..

If Person = 1 or 4-19, then Father = 3 and Mother = 2
If Person = 2, then Father = 23 and Mother = 22
If Person = 3, then Father = 33 and Mother = 32
If Person = 22,23,32 or 33, Father and Mother = 0

Following is the hash that I have used.

my %relations = (
1 => { Father => 3, Mother => 2 },
2 => { Father => 23, Mother => 22 },
3 => { Father => 33, Mother => 32 },
22 => { Father => 0, Mother => 0 },
);
$relations{'23'} = $relations{'32'} = $relations{'33'} = $relations{'22'};
for my $person ( 4 .. 19 ) {
$relations{$person} = $relations{1};
}

I am sure there will be a better ways to do it.. Any help will be greatly
appreciated..

I'm not quite sure if this helps, but I had a similar problem once. That
problem couldn't, however, be solved as simple as your approach, so I had
to create a function to "serialize" a "string of numbers" into an array of
representative numbers.

Example: I could give the function a string like '1 2 3,4,5 6-10', and it
would return an array with the numbers 1 through 10.

Here's how it goes;

sub serialize {
my $string = shift;

my @numbers = ();
if ( defined $string && length $string ) {
$string =~ s,\s+-\s+,-,g;
foreach ( split(/\s+/, $string) ) {
foreach ( split(/,/) ) {
if ( /(\d+)-(\d+)/ ) {
my $from = $1;
my $to = $2;
for ( $from .. $to ) {
push( @numbers, int($_) );
}
}
else {
push( @numbers, int($_) );
}
}
}
}

my @sorted = sort { $a <=> $b } @numbers;
return unique_array( \@sorted );
}

I guess you notice the reference to the 'unique_array' function. It is
also a function in the same package, roughly the same as the one described
in the Perl FAQ ('perldoc -q duplicate').
 
B

Ben Morrow

Cognition Peon said:
I have used a hash to store relations between persons and their parents
in a study that we are doing.. The relations are defined by the following
conditions..

If Person = 1 or 4-19, then Father = 3 and Mother = 2
If Person = 2, then Father = 23 and Mother = 22
If Person = 3, then Father = 33 and Mother = 32
If Person = 22,23,32 or 33, Father and Mother = 0

Following is the hash that I have used.

my %relations = (
1 => { Father => 3, Mother => 2 },
2 => { Father => 23, Mother => 22 },
3 => { Father => 33, Mother => 32 },
22 => { Father => 0, Mother => 0 },
);
$relations{'23'} = $relations{'32'} = $relations{'33'} = $relations{'22'};
for my $person ( 4 .. 19 ) {
$relations{$person} = $relations{1};
}

I am sure there will be a better ways to do it.. Any help will be greatly
appreciated..

I'm not sure what you mean by 'efficient' here: the relevant question is
'is this the right data structure for what I want to do with the data?'.
For instance, for some applications you would need to build, for each
Person, a list of their children: this can be done starting from the
data structure you have there, of course.

Since the Persons are all numeric, you may wish to consider an array
rather than a hash. I tend to use arrays when things have a defined
order, so for instance if I might be wanting to ge over all Persons in
numerical order; I would use a hash when the concept is 'set' rather
than 'list'.

One potential problem here is that $relations{1} and $relations{4} are
references to the same anonymous hash, which means changes to one will
change the other as well: are these semantics correct, or not?

If they aren't, then I would have coded it more like:

my %relations;
for (@relations{ 1, 4..19 }) {
$_ = { Father => 3, Mother => 2 };
}
for (@relations{ 2 }) {
$_ = { Father => 23, Mother => 22 };
}
for (@relations{ 3 }) {
$_ = { Father => 33, Mother => 32 };
}
for (@relations{ 22, 23, 32, 33 }) {
$_ = { Father => 0, Mother => 0 };
}

which will create a new anon array for each entry.

Ben
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top