Scaling up sample data in an array of hashes 
Author Message
 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



Sun, 27 Jul 2003 21:28:53 GMT  
 Scaling up sample data in an array of hashes


Quote:
> 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?

You could use map to create the new hash with something like

my($i,$tmpval);

my %newhash = map {

  ($_, $patients[$tmpval]{$_} )
  } qw(lastname ename1 firstname ename2 fathersnam ename3 birthdata);

++$max_id;
$newhash{patcode} = $max_id;

On each iteration of map, a new array index is picked on the 1st, 3rd, and
5th elements, then the key -> value pair is added to the hash

Quote:
> 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.)

They are the same - the first is best in my eyes because it makes
multidimensional referencing much clearer, esp when you start getting into
stuff like $ref->[1]{John}[3] etc etc

--Ben Kennedy



Wed, 30 Jul 2003 11:35:01 GMT  
 Scaling up sample data in an array of hashes


Quote:
>You could use map to create the new hash with something like

>my($i,$tmpval);

>my %newhash = map {

>  ($_, $patients[$tmpval]{$_} )
>  } qw(lastname ename1 firstname ename2 fathersnam ename3 birthdata);

>++$max_id;
>$newhash{patcode} = $max_id;

>On each iteration of map, a new array index is picked on the 1st, 3rd, and
>5th elements, then the key -> value pair is added to the hash

Thanks for responding, Ben. it took me some time to figure out how
this works - a valuable piece of Perl education for me.

As it stands, it's not quite right, but changing $i++ to ++$i does the
trick. It's certainly much neater (and niftier) than what I had.

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



Sat, 02 Aug 2003 20:19:33 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. asap : Array/hashes/array small sample

2. URGENT: Hash in Hash, Array in Array, Array in Hash, Hash in Array

3. hash of values and array of hashes of values and array of hashes

4. Data::Dump an array of hashes.

5. Accessing data in a hash of arrays

6. Canvas->Scale and Tk::Scale conflict?

7. Tied hash not scaling - advice?

8. array/hash to file to array/hash

9. Simple Perl5 question (hash-array of hash-array refs)

10. question regarding hash hash hash of array

11. Perlmenu/DBI-Mysql Data-Entry/Edit sample searched

12. postscript phone book (Sample data file)

 

 
Powered by phpBB® Forum Software