Scaling up sample data in an array of hashes
Hi. I have a medical database with random names and stuff that I'm
using in a simulation. I want to scale it up so that the number of
entries is greater by some factor.
I read the existing data into an array of hashes and then use the
following code to pump up the array, using random recombinations of
the existing data, before writing it back to a new table. It's
complicated by the fact the database is bilingual, with Greek and
Latin alphabet versions of the names, so those fields have to remain
in sync, hence the pairs of lines in the $rec assignments.
I'm quite pleased with myself for managing this at all, actually, but
I can't help thinking that there might be a niftier way to do it,
maybe using grep or map. Any ideas?
Code follows - warnings and strictures on, naturally. $max_id is a
unique id code which is set to the maximum value of the original data
set.
# start of code
my $rec = {};
my $rnd;
++$max_id;
$rec->{ patcode } = $max_id;
$rec->{ lastname } = ${ $patients[ $rnd ] }{ lastname };
$rec->{ ename1 } = ${ $patients[ $rnd ] }{ ename1 };
$rec->{ firstname } = ${ $patients[ $rnd ] }{ firstname };
$rec->{ ename2 } = ${ $patients[ $rnd ] }{ ename2 };
$rec->{ fathersnam } = ${ $patients[ $rnd ] }{ fathersnam };
$rec->{ ename3 } = ${ $patients[ $rnd ] }{ ename3 };
$rec->{ birthdate } = ${ $patients[ $rnd ] }{ birthdate };
Quote:
}
# end of code
By the way, the following lines are equivalent, right?
$var->{ lastname } = 'foo';
${ $var }{ lastname } = 'foo';
$$var{ lastname } = 'foo';
Is there any reason for using one syntax rather than the other? (The
last is the shortest, the first is the clearest, but the middle one is
the prettiest - at least to my eye.)
All suggestions appreciated.
Phil
--
Philip Lees
ICS-FORTH, Heraklion, Crete, Greece
Ignore coming events if you wish to send me e-mail
'The aim of high technology should be to simplify, not complicate' - Hans Christian von Baeyer